2011-08-02 46 views
0

我正在實施基於音頻的應用程序。我用兩個AVPlayers播放兩種不同的聲音。一旦聲音播放,我需要做不同的動作。爲此,我使用了NSNotifications。但我的問題是我無法找到與哪個播放器相關的通知。我的通知代碼和選擇器代碼如下,請任何人告訴我我做了什麼錯誤。帶參數的NSNotificationCenter

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playingItemDidEnd:) 
              name:AVPlayerItemDidPlayToEndTimeNotification 
              object:iPodPlayer]; 


[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playingItemDidEnd:) 
              name:AVPlayerItemDidPlayToEndTimeNotification 
              object:applicationPlayer ]; 

- (void)playingItemDidEnd:(NSNotification *)notification 
{ 
     id object= [notification object]; 

    if(object==ipodPlayer) 
    { 
     printf("\n Notification from iPod Player "); 

    } 
    else if(object==applicationPlayer) 
    { 
     printf("\n Notification from application Player "); 
    } 

}

在此先感謝, 錢德拉。

回答

3

我需要如下改變代碼的基礎上,

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playingItemDidEnd:) 
              name:AVPlayerItemDidPlayToEndTimeNotification 
              object:[applicationPlayer currentItem] ]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playingItemDidEnd:) 
              name:AVPlayerItemDidPlayToEndTimeNotification 
              object:[iPodPlayer currentItem]]; 

和選擇代碼應該如下,

- (void)playingItemDidEnd:(NSNotification *)notification 
{ 

    AVPlayerItem* object= [notification object]; 
    if(object==[applicationPlayer currentItem]) 
    { 

    } 
    else if(object==[avPlayer currentItem]) 
    { 

    } 
}