MPMoviePlayerPlaybackDidFinishNotification gets ca

2020-05-29 08:40发布

According to Apple's MPMoviePlayerController doc:

MPMoviePlayerPlaybackDidFinishNotification - This notification is not sent in cases where the movie player is displaying in fullscreen mode and the user taps the Done button.

Seems to me this is dead wrong. Using the code below, playerPlaybackDidFinish gets called when I tap the done button.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];

- (void) playerPlaybackDidFinish:(NSNotification*)notification
{
    NSLog(@"WHY?");
    self.player.fullscreen = NO;
}

I need to distinguish between the user tapping the done button and the movie finishing all the way through playback. playerPlaybackDidFinish does get called when the movie ends, but like I said it also gets called when you tap Done.

4条回答
疯言疯语
2楼-- · 2020-05-29 09:14

Here is how you check the MPMoviePlayerPlaybackDidFinishReasonUserInfoKey which is part of the notification of MPMoviePlayerPlaybackDidFinishNotification

- (void) playbackDidFinish:(NSNotification*)notification {
    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (reason == MPMovieFinishReasonPlaybackEnded) {
        //movie finished playin
    }else if (reason == MPMovieFinishReasonUserExited) {
        //user hit the done button
    }else if (reason == MPMovieFinishReasonPlaybackError) {
        //error
    }
}
查看更多
再贱就再见
3楼-- · 2020-05-29 09:23

When you get the notification you can check the player's endPlaybackTime. If it's -1 then the movie finished all the way back naturally.

For streamed content, you can check the MPMoviePlayerPlaybackDidFinishReasonUserInfoKey inside the userInfo on the MPMoviePlayerPlaybackDidFinishNotification.

If it's equal to MPMovieFinishReasonUserExited then it's the user stopped playing the content.

查看更多
我想做一个坏孩纸
4楼-- · 2020-05-29 09:25

Make sure for

    moviePlayer.repeatMode = MPMovieRepeatModeNone;
查看更多
走好不送
5楼-- · 2020-05-29 09:29

I am using the following to do something when a movie is played all the way to the end:

- (void)playbackDidFinish:(NSNotification*)notification
{
    BOOL playbackEnded = ([[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue] == MPMovieFinishReasonPlaybackEnded);
    BOOL endReached = (self.player.currentPlaybackTime == self.player.playableDuration);

    if (playbackEnded && endReached) {
        // Movie Ended
    }
}
查看更多
登录 后发表回答