2013-04-27 74 views
5

我在播放視頻後立即顯示文本。我正在使用通知技術來實現這一點。唯一的問題是觀察者偶爾會被調用兩次。它觸發兩次「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]; 

}

+0

您是否在視頻播放按鈕操作中添加了通知觀察者? – NSUserDefault 2013-04-27 04:55:55

+0

@NSUserDefault,沒有按鈕。我只是在滑動導航。顯示頁面後播放動畫。 – Armand 2013-04-27 04:58:39

+0

你在哪裏調用 - (void)playAnimation:(NSString *)theString方法? – NSUserDefault 2013-04-27 05:05:15

回答

7

試試這個,

-(void)itemDidFinishPlaying { 

     [self displayContent: pageContent]; 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem]; 

} 

它可能爲你工作。

EDIT1:

刪除通知觀察者的每次你設置新one.Try以下scenario.It將確保之前,刪除以前的觀察者(甚至不存在沒有問題)和增加了新的。

// Subscribe to the AVPlayerItem's DidPlayToEndTime notification. 

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem]; 
+0

對不起,它沒有解決。 – Armand 2013-05-05 20:04:51

+0

現在我正在用條件語句解決它,只打印一次文本。所以如果通知連續第二次發生,它只會顯示文本一次。不是最性感的方法,但現在它允許我前進。 – Armand 2013-05-05 20:31:25

+0

它到目前爲止工作與此行...謝謝[[NSNotificationCenter defaultCenter] removeObserver:self]; – Armand 2013-05-05 23:14:27

1

這隻發生在AirPlay期間。我正在使用以下解決方法來忽略重複的通知。

if ([notificaiton.object isEqual:self.player.currentItem] && (self.player.rate > 0)) 
{ 
    [self.player pause]; 

    //Do your stuff here. 
} 

NSUserDefault的回答也工作但你必須再次添加觀察員和可以根據您的設置很複雜。

相關問題