我这样设置电影播放器:
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:@"whatever.mp4"];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.shouldAutoplay = YES;
[self.moviePlayer prepareToPlay];
self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
self.moviePlayer.view.frame = self.container.bounds;
self.moviePlayer.view.userInteractionEnabled = NO;
[self.container addSubview:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(moviePlayBackDidFinish:) name: MPMoviePlayerPlaybackStateDidChangeNotification
object: self.moviePlayer];
通知对于保持播放器循环是必要的,因为 repeatMode 几乎没用(视频会重复一两次,可能会重复几次,具体取决于中国的大米价格,然后停止)。因此,要保持视频循环播放,您必须这样做:
- (void)moviePlayBackDidFinish:(NSNotification *)note {
if (note.object == self.moviePlayer) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
[self.moviePlayer play];
}
}
}
现在的问题是我需要能够暂停视频。出于某种原因,调用
[self.moviePlayer pause];
导致通知被触发原因 == MPMovieFinishReasonPlaybackEnded 即使文档清楚地说明了这一点:
Constants
MPMovieFinishReasonPlaybackEnded
The end of the movie was reached.
Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.
电影的结尾没有到达。我只是叫暂停:。所以通知被触发,因此电影再次播放,否定了暂停动作。
所以你可以看到问题。如何成功暂停循环播放的视频?