MPMoviePlayerPlaybackDidFinishNotification gets called when it shouldn't

自古美人都是妖i 提交于 2019-12-03 03:39:11

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!