2

*我正在创建一个 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;
4

2 回答 2

1

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

于 2012-04-17T14:12:09.267 回答
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];
    }        
}

还是谢谢你

于 2012-04-19T08:22:13.260 回答