带参数的NSNotificationCenter

时间:2011-08-02 05:21:00

标签: ios4 nsnotifications nsnotificationcenter

我正在实施基于音频的应用程序。在那里我使用两个AVPlayers播放两种不同的声音。一旦发出声音,我需要做不同的动作。为此,我使用了NSNotifications。但我的问题是我无法找到与哪个播放器相关的通知。我的通知代码和选择器代码如下,请任何人建议我做错了什么。

[[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 ");
     }

}

提前致谢, 钱德拉。

1 个答案:

答案 0 :(得分: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])
    {

    }
}