尝试制作电影循环,但 MPMoviePlayerPlaybackDidFinishNotification 在视频结束时永远不会调用。
这是我的代码:
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"dent4" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view setFrame:CGRectMake(playButton.frame.origin.x,
playButton.frame.origin.y,
playButton.frame.size.width,
playButton.frame.size.height)];
self.moviePlayerController.view.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
self.moviePlayerController.shouldAutoplay = YES;
self.moviePlayerController.initialPlaybackTime = 0;
[self.moviePlayerController setFullscreen:NO];
self.moviePlayerController.controlStyle = MPMovieControlStyleNone;
self.moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
self.moviePlayerController.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview:self.moviePlayerController.view];
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController play];
视频正确播放一次,但不触发结束事件。
- (void)moviePlaybackComplete:(NSNotification *)note
{
NSLog(@"END");
if (note.object == self.moviePlayerController) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
NSLog(@"REPLAY");
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController play];
}
}
}
另一件事奇怪的视频在模拟器上正确循环(但 moviePlaybackComplete 从不调用)但在设备上只播放一次。
任何人都可以帮助我吗?我已经看到一些帖子在全屏上说它没有被调用,但我不在全屏模式下。