如何在同一个MPMoviePlayerController中播放另一个视频?

时间:2012-04-17 09:57:22

标签: iphone objective-c ios mpmovieplayercontroller

*我正在创建一个可以观看视频的iPhone应用程序... 我一直在制作自己的控件,所以我想实现下一个,后退按钮,我可以显示主题所以对于UI一切都很好问题是重新启动MoviePlayer与另一个内容... 任何想法???

我可以在播放完成时使用相同的配置,我的意思是......如果视频结束播放开始播放另一个... 我试图设置contentUrl但它有一个例外:

2012-04-17 11:37:41.198 NexTest2[8218:11f03] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 11.5]'

viewController.m

- (void)viewDidLoad{
[super viewDidLoad];    
fileURL = [NSURL URLWithString:urlString];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
moviePlayerController.controlStyle = MPMovieControlStyleNone;
[moviePlayerController setShouldAutoplay:YES];
[self addObservers];   

/* Inset the movie frame in the parent view frame. */
CGRect viewInsetRect = CGRectInset ([self.view bounds],0.0, 0.0 );
[[moviePlayerController view] setFrame: viewInsetRect ];
[self.view addSubview:moviePlayerController.view]; 
[self resizeControlViews];
}

然后在resizeControlViews我有这个,因为我使用这两个视图像控件:

-(void)resizeControlViews{
CGRect barFrame = self.controlBarView.frame;
barFrame.origin.x = round((moviePlayerController.view.frame.size.width - barFrame.size.width) / 2.0);
barFrame.origin.y = round((moviePlayerController.view.frame.size.height - barFrame.size.height)/10.0);
self.controlBarView.frame = barFrame;
[moviePlayerController.view addSubview:controlBarView];
CGRect playFrame = self.controlPlaybackView.frame;
playFrame.origin.x = round((moviePlayerController.view.frame.size.width - playFrame.size.width) / 2.0);
playFrame.origin.y = round((moviePlayerController.view.frame.size.height - playFrame.size.height)/1.1);
self.controlPlaybackView.frame = playFrame; 
[moviePlayerController.view addSubview:controlPlaybackView];
}

这里一切正常,在其中一个视图中有一个控制搜索的滑块,另一个是播放,下一个和后一个按钮。 我想问题可能就是这些视图的UI组件......

我添加了这些观察者来控制播放:

//Add the observers to the player
-(void)addObservers{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStarted:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setDurationLabel:) name:MPMovieDurationAvailableNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyWindowChanged:) name:UIWindowDidBecomeKeyNotification object:nil];

}

所以要在播放器播放时进行处理我使用MPMoviePlayerPlaybackStateDidChangeNotification与此选择器:

//called when the playback state changes of state
- (void)playbackStarted:(NSNotification*)notification {
MPMoviePlayerController *player = notification.object;
if (player.playbackState == MPMoviePlaybackStatePlaying) {
    [self timerRunning];
}
}

如果玩家正在“玩”,我会用这种方法监控:

-(void)timerRunning{
self.currentTime.text = [self floatToStringTime:(moviePlayerController.currentPlaybackTime)];
self.timeBar.value = moviePlayerController.currentPlaybackTime / moviePlayerController.duration;

if (moviePlayerController.playbackState == MPMoviePlaybackStatePlaying) {
    [self performSelector:@selector(timerRunning) withObject:nil afterDelay:0.5];
}        
}

所以也许这就是问题......但是通过一个视频,它可以完美地运作......

我有四个错误的地方......就在我在线上监控以设置滑块的值,但我真的不知道为什么,我认为这是因为另一个视频或停止视频它仍然被执行,所以播放器没有持续时间或当前播放什么让这个例外...我怎么能解决它...这是错误...可能将此代码移动到另一部分......

self.timeBar.value = moviePlayerController.currentPlaybackTime / moviePlayerController.duration;

2 个答案:

答案 0 :(得分:1)

检查您的UI相关代码。在获取和使用与内容相关的属性之前,您很可能没有正确检查玩家状态。

答案 1 :(得分:1)

最后它工作得很好... setCurrentContentUrl工作正常,问题是我的代码所以我解决了...我只是验证持续时间不同于0 ... 现在我有一个完全自定义控件的玩家:)很酷:))

-(void)timerRunning{   
    if (moviePlayerController.playbackState == MPMoviePlaybackStatePlaying) {       
        self.currentTime.text = [self floatToStringTime:(moviePlayerController.currentPlaybackTime)];
        if (moviePlayerController.duration != 0) {
            self.timeBar.value = moviePlayerController.currentPlaybackTime / moviePlayerController.duration;
        }

    [self performSelector:@selector(timerRunning) withObject:nil afterDelay:0.5];
    }        
}

谢谢你