开发者

MPMoviePlayerPlaybackDidFinishNotification gets called when it shouldn't

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.

[[N开发者_运维知识库SNotificationCenter 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.


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


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


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.


Make sure for

    moviePlayer.repeatMode = MPMovieRepeatModeNone;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜