我在播放視頻後立即顯示文本。我正在使用通知技術來實現這一點。唯一的問題是觀察者偶爾會被調用兩次。它觸發兩次「itemDidFinishPlaying」(並因此觸發相同名稱的方法)。我無法預測何時。我不知道爲什麼。它看起來是隨機的(我知道這聽起來很奇怪),就像它工作正常一樣,讓我們說連續15次,下一次這種行爲發生在藍色之外。我重建並運行應用程序,並且這次它連續運行19次,然後再調用Observer兩次,等等......不可預知。我已經嘗試過每種情況來預測錯誤以修復它。到目前爲止,這是不可能的。所以我有兩個問題。NSNotification中的觀察者(itemDidFinishPlaying)隨機調用兩次
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 2013-04-27 04:55:55
@NSUserDefault,沒有按鈕。我只是在滑動導航。顯示頁面後播放動畫。 – Armand 2013-04-27 04:58:39
你在哪裏調用 - (void)playAnimation:(NSString *)theString方法? – NSUserDefault 2013-04-27 05:05:15