根据Apple的MPMoviePlayerController doc:
MPMoviePlayerPlaybackDidFinishNotification - 如果电影播放器以全屏模式显示并且用户点击完成按钮,则不会发送此通知。
对我来说这是错误的。使用下面的代码,当我点击完成按钮时,会调用playerPlaybackDidFinish。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];
- (void) playerPlaybackDidFinish:(NSNotification*)notification
{
NSLog(@"WHY?");
self.player.fullscreen = NO;
}
我需要区分用户点击完成按钮和电影完成播放的所有方式。当电影结束时,会调用playerPlaybackDidFinish,但就像我说的那样,当你点击Done时它也会被调用。
答案 0 :(得分:22)
以下是检查MPMoviePlayerPlaybackDidFinishReasonUserInfoKey的方法,这是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
}
}
答案 1 :(得分:3)
当电影一直播放到最后时,我正在使用以下内容来做某事:
- (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
}
}
答案 2 :(得分:2)
收到通知后,您可以查看播放器的endPlaybackTime。如果它是-1那么电影就会自然地完成。
对于流内容,您可以检查MPMoviePlayerPlaybackDidFinishNotification上userInfo内的MPMoviePlayerPlaybackDidFinishReasonUserInfoKey。
如果它等于MPMovieFinishReasonUserExited,那么用户就是停止播放内容了。
答案 3 :(得分:0)
确保
moviePlayer.repeatMode = MPMovieRepeatModeNone;