我用MPMoviePlayerController
播放视频。这是代码。
// Getting URL from path
NSURL *url = [NSURL fileURLWithPath:self.moviePlayingTempPath];
// Initialize the movie player view controller with a video URL string
self.playerVC = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:self.playerVC
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerVC];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerVC];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moveiPlayBackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.playerVC];
[self.playerVC setControlStyle:MPMovieControlStyleEmbedded];
self.playerVC.view.frame = CGRectMake(0, 0, 1024, 648);
[self.view addSubview:self.playerVC.view];
// Start playback
[self.playerVC prepareToPlay];
[self.playerVC play];
它在嵌入模式下工作正常。我在全屏模式下有非常不寻常的行为。当我全屏播放并且可以播放视频没有任何问题。然后我可以单击完成按钮并毫无问题地进入嵌入模式。
全屏显示并单击向前或向后搜索按钮时出现问题。然后玩家停止播放并显示带有加载文本的活动指示器。然后,如果我单击完成按钮,则播放器将永远消失。我无法弄清楚这个确切的问题。实际上我不需要向前和向后寻找按钮。我们可以阻止寻求或如何解决这个问题。
答案 0 :(得分:0)
我通过在用户从全屏切换时删除并添加电影播放器来解决此问题。
这是完整的代码
-(void)continuePlayingFile{
NSURL *url = [NSURL fileURLWithPath:self.moviePlayingTempPath];
// Initialize the movie player view controller with a video URL string
self.playerVC = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:self.playerVC
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerVC];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerVC];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moveiPlayBackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.playerVC];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willEnterFullScreen:)
name:MPMoviePlayerWillEnterFullscreenNotification
object:self.playerVC];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willExitFullScreen:)
name:MPMoviePlayerWillExitFullscreenNotification
object:self.playerVC];
[self.playerVC setControlStyle:MPMovieControlStyleEmbedded];
self.playerVC.movieSourceType = MPMovieSourceTypeFile;
self.playerVC.view.frame = CGRectMake(0, 0, 1024, 648);
[self.view addSubview:self.playerVC.view];
// Start playback
[self.playerVC prepareToPlay];
[self.playerVC play];
}
-(void)willExitFullScreen:(NSNotification *)note{
self.isMovieGoFullScreen = NO;
if(self.isUserClickNextButton){
//This is to handle user click on next/previous button and then click done button.
self.isUserClickNextButton=NO;
[self continuePlayingFile];
}
}
-(void)moveiPlayBackStateChanged:(NSNotification*)aNotification{
if(self.playerVC){
self.currentPlayerState = self.playerVC.playbackState;
if(self.currentPlayerState == MPMoviePlaybackStateStopped){
self.isUserClickNextButton =YES;
}
}
}
答案 1 :(得分:0)
请注意,添加观察者时,object参数应为playerVC.moviePlayer,而不是playerVC。
moviePlayer(类MPMoviePlayerController)是发送通知的人。
所以改变这个:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayerVC];
到此:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayerVC.moviePlayer];