ビデオの再生が終了するとすぐにテキストを表示しています。私はこれを達成するための通知方法を使用しています。唯一の問題は、Observerが毎回2回呼び出されることです。これは "itemDidFinishPlaying"を2回(したがって同じ名前のメソッド)トリガします。私はいつ予測できないのですか。どうしてか分かりません。ランダムに見える(私はこれが奇妙に聞こえる)。うまく動いているように、15回連続して言いましょう。私はアプリケーションを再構築して実行し、今度はObserverを2回呼び出す前に19回連続してうまく動作します...予測不能です。私はそれを修正するためにバグを予測するためにあらゆるシナリオを試しました。今まで不可能だった。だから私は2つの質問があります。NSNotificationのオブザーバ(itemDidFinishPlaying)2回呼び出されるランダムに
1)なぜ、「ランダムに」起こるのですか?
2)このダブルコール問題を解決するにはどうすればよいですか?
また、これらの2次の会話は助けていない:
Why the Observer in NSNotification called twice....?
How to stop the Observer in NSNotification to called twice?
を以下の私のコードを見つけてください:
- (void) playAnimation: (NSString *) theString {
UIView *thisCurrentView = self.currentView;
UIView *thisReplacementView = [[UIView alloc] init];
//[avPlayer pause];
[self replaceView: thisCurrentView withView: thisReplacementView];
NSString *filepath = [[NSBundle mainBundle] pathForResource:theString ofType:@"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
// First create an AVPlayerItem
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:fileURL];
// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
// Pass the AVPlayerItem to a new player
controlledPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerLayer *animatedLayer = [AVPlayerLayer playerLayerWithPlayer:controlledPlayer];
[animatedLayer setFrame:CGRectMake(0, 0, 1024, 1024)];
[thisReplacementView.layer addSublayer: animatedLayer];
// Begin playback
[controlledPlayer play];
// Clear some content
[self displayNoContent];
pageContent = theString;
playingStatus = YES;
}
を - (無効)itemDidFinishPlaying {
[self displayContent: pageContent];
}
ビデオ再生ボタン操作で通知オブザーバを追加していますか? – NSUserDefault
@NSUserDefault、ボタンなし。私はナビゲートするためにスワイプしています。ページが表示されたらアニメーションが再生されます。 – Armand
あなたがこれを呼び出す場所 - (void)playAnimation:(NSString *)theStringメソッド? – NSUserDefault