Done button event MPMoviePlayerController

33,587

Solution 1

It worked for me on iPad when I listen to MPMoviePlayerWillExitFullscreenNotification.

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(doneButtonClick:) 
                                             name:MPMoviePlayerWillExitFullscreenNotification 
                                           object:nil];

And selector method:

-(void)doneButtonClick:(NSNotification*)aNotification{
    NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

    if ([reason intValue] == MPMovieFinishReasonUserExited) {
        // Your done button action here
    }
}

Solution 2

check for a enum inside notification userInfo dictionary

NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

if ([reason intValue] == MPMovieFinishReasonUserExited) {

   // done button clicked!

}

Selected answer has since integrated my response. Please refer above.

Solution 3

SUCCESSFULLY TESTED IN iOS7 AND iOS8

Check and remove earlier notification observer if any for MPMoviePlayerPlaybackDidFinishNotification.

- (void)playVideo:(NSString*)filePath
{
     // Pass your file path
        NSURL *vedioURL =[NSURL fileURLWithPath:filePath];
        MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];

    // Remove the movie player view controller from the "playback did finish" notification observers
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:playerVC.moviePlayer];

    // Register this class as an observer instead
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:playerVC.moviePlayer];

    // Set the modal transition style of your choice
    playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    // Present the movie player view controller
    [self presentViewController:playerVC animated:YES completion:nil];

    // Start playback
    [playerVC.moviePlayer prepareToPlay];
    [playerVC.moviePlayer play];
}

Dismiss controller

- (void)movieFinishedCallback:(NSNotification*)aNotification
{
    // Obtain the reason why the movie playback finished
    NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

    // Dismiss the view controller ONLY when the reason is not "playback ended"
    if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
    {
        MPMoviePlayerController *moviePlayer = [aNotification object];

        // Remove this class from the observers
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];

        // Dismiss the view controller
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

You'r Done !!!

Solution 4

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(doneButtonClick:) 
                                             name:MPMoviePlayerDidExitFullscreenNotification 
                                           object:nil];

- (void)doneButtonClick:(NSNotification*)aNotification
{
    if (self.moviePlayer.playbackState == MPMoviePlaybackStatePaused)
    {
        NSLog(@"done button tapped");
    }
    else
    {
        NSLog(@"minimize tapped");
    }
}

Solution 5

Swift version, for anyone who's interested:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerDoneButtonClicked:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)

Notification handler:

func moviePlayerDoneButtonClicked(note: NSNotification) {

    let reason = note.userInfo?[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]
    if (MPMovieFinishReason(rawValue: reason as! Int) == MPMovieFinishReason.UserExited) {
        self.exitVideo()
    }
}
Share:
33,587
w00
Author by

w00

Updated on February 16, 2020

Comments

  • w00
    w00 over 4 years

    On my iPhone i'm playing video/audio files in fullscreen mode. When the video/audio file reached its end then the following method is triggered:

    - (void) movieFinishedCallback:(NSNotification*) aNotification {
        MPMoviePlayerController *player = [aNotification object];
    
        [player stop];
    
        [[NSNotificationCenter defaultCenter] 
            removeObserver:self
            name:MPMoviePlayerPlaybackDidFinishNotification
            object:player];
    
        [player autorelease];
        [moviePlayer.view removeFromSuperview];
    
        NSLog(@"stopped?");
    }
    

    That works fine! But the problem is when i press on the "Done" button when the video/audio file is still playing. Then this method doesn't get triggered...

    Anyone any idea how to catch the event when the "Done" button is pressed? Because right now media player stays in the view. Its not disappearing.