*私はビデオを見ることができる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にこれがあります。これは、コントロールのように次の2つのビューを使用しているためです。
-(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];
}
ここではすべてが正常に機能しています。これらのビューの1つにはシークを制御するスライダーがあり、もう1つには再生、次、戻るボタンがあります。おそらく問題は、これらのビューの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];
}
}
だから多分ここに問題があります...しかし、単一のビデオでそれは完璧に機能します...
私は間違いが4つあります...スライダーに値を設定するために行を監視している間ですが、理由は本当にわかりません。別のビデオまたは停止中のビデオが原因だと思いますまだ実行されているので、プレーヤーにはこの例外を引き起こす期間や現在の再生がありません...どうすれば解決できますか...ここに間違いがあります...おそらくこのコードを別の部分に移動します...
self.timeBar.value = moviePlayerController.currentPlaybackTime / moviePlayerController.duration;