How to stop MPMoviePlayerViewController's automatic dismiss on moviePlaybackDidFinish?

后端 未结 4 1992
渐次进展
渐次进展 2020-12-17 00:45

An MPMoviePlayerViewController which is presented modally through presentMoviePlayerViewControllerAnimated: automatically dismisses itself when it\'s content fi

4条回答
  •  萌比男神i
    2020-12-17 01:03

    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];
        }
    }
    

提交回复
热议问题