Continue playing movie in MPMoviePlayerViewControl

2020-02-16 02:56发布

问题:

I have an MPMoviePlayerViewController that plays a movie in my app.

Multitasking is enabled so that I can return to the home screen or lock the device and then use the music controls to continue playing audio. The problem is the fact that audio is stopped when the app loses focus, so that you have to manually press play to start it again.

The default music app, of course, and some others such as Ambiance continue playing audio in the background however these only ever play audio whereas my app's audio source is a video - is it possible to keep playing when losing focus from a MPMoviePlayerViewController?

回答1:

This works for me. Load a false url when video player done button pressed. For Example

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:yourViewController];

and implement

- (void)moviePlaybackComplete:(NSNotification *)notification
{
    self.playerViewController=[self.playerViewController initWithContentURL:[NSURL URLWithString:@"dummyUrl"]];
    [self.playerViewController.moviePlayer stop];
    MPMoviePlayerViewController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];
}

This works just fine. Thanks