I have set App Plays Audio for Required background modes in my app's info.plist to keep the mpmovieplayerviewcontroller not dismissing after going to background. It did a great job. However, Apple has rejected my app's update recently, as I set that attribute.
Then I keep finding solutions for doing the same job but fail. To deal with it, I save the currentplaybacktime when the view controller receive UIApplicationDidEnterBackgroundNotification
-(void)willEnterBackground {
NSLog(@"willEnterBackground");
if (self.mp) {
playbackTime = self.mp.moviePlayer.currentPlaybackTime;
[self.mp dismissModalViewControllerAnimated:YES];
}
}
And set the currentPlayBackTime after it receives UIApplicationWillEnterForegroundNotification:
- (void)willEnterForeground {
NSLog(@"willEnterForeground");
if (!self.mp)
return;
if(self.mp.moviePlayer.playbackState == MPMoviePlaybackStateInterrupted || self.mp.moviePlayer.playbackState == MPMoviePlaybackStateStopped || self.mp.moviePlayer.playbackState == MPMoviePlaybackStatePaused)
{
[self continuePlayback];
}
}
- (void)continuePlayback {
NSLog(@"%f", self.mp.moviePlayer.duration);
NSLog(@"%f", playbackTime);
[self.mp.moviePlayer stop];
self.mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[self.videos objectForKey:@"medium"]]] ;
[self.mp.moviePlayer setInitialPlaybackTime:playbackTime];
[self.mp.moviePlayer play];
[self presentModalViewController:self.mp animated:YES];
}
It works, but with some trade off: As I re-init it, it lost the streamed video part. Users need to wait for streaming again.
And here comes my questions:
- Is there a way that keep streaming Youtube's video when the app is in background?
- or how to keep the streamed part?
It would play from the beginning of the video if I skip the line:
self.mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[self.videos objectForKey:@"medium"]]] ;
in continuePlayBack method or I setCurrentPlayBackTime instead of setInitPlayBackTime.