Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop MPMoviePlayerViewController's automatic dismiss on moviePlaybackDidFinish?

An MPMoviePlayerViewController which is presented modally through presentMoviePlayerViewControllerAnimated: automatically dismisses itself when it's content finishes playing.

I've tried to disable this, since I want to play other content afterwards. However, even if I register to the NSNotificationCenter with [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer]; and set some other content, it still dismisses.

How can I stop MPMoviePlayerViewController from automatically dismissing itself?

UPDATE:

As a clarification, this question is only about removing the automatic dismissal and not about dealing with the disabled 'done' button. The selected answer reflects. This is by design, since we assume the developer adds their own means of dismissing the MPMoviePlayerViewController. However, @bickster's answer deals with the 'done' button as well.

like image 468
SpacyRicochet Avatar asked Nov 16 '12 16:11

SpacyRicochet


3 Answers

Thanks to this blog article I figured out that MPMoviePlayerViewController automatically registers itself to the NSNotificationCenter upon creation. You have to first remove this registration and it will stop dismissing itself automatically.

// Initialize the movie player view controller with a video URL string
MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:playerVC  name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer];
like image 51
SpacyRicochet Avatar answered Jan 08 '23 19:01

SpacyRicochet


You can use this code to stop the viewcontroller from automatically dismissing and capture the event when the user clicks the "Done" button so you can dismiss the viewcontroller yourself.

Step 1. - alloc a MPMoviePlayerViewController

videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[[NSURL alloc ]initWithString:[aURL];

Step 2. - Remove the default MPMoviePlayerPlaybackDidFinishNotification observer and add your own

[[NSNotificationCenter defaultCenter] removeObserver:videoPlayer
name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer.moviePlayer];

Step 3. - Present viewcontroler

[self presentMoviePlayerViewControllerAnimated:videoPlayer];

Step 4. - Add videoFinish: method

-(void)videoFinished:(NSNotification*)aNotification{
    int value = [[aNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (value == MPMovieFinishReasonUserExited) {
        [self dismissMoviePlayerViewControllerAnimated];
    }
}
like image 28
bickster Avatar answered Jan 08 '23 21:01

bickster


You can try something like this.

when the mpmovieplayercontroller finishes playing a video and you recieve the notification in your method movieFinishedCallback: implemect

       [playerVC.movieplayer setContentURL:// set the url of the file you want to play here];

       [playerVC.moviePlayer play];

Hope this helps

like image 24
Jasmeet Singh Avatar answered Jan 08 '23 21:01

Jasmeet Singh