*我正在创建一个 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]'
视图控制器.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;