0

我正在实现一个基于音频的应用程序。我正在使用两个 AVPlayer 播放两种不同的声音。一旦声音播放,我需要做不同的动作。为此,我使用了 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 ");
     }

}

在此先感谢,钱德拉。

4

1 回答 1

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])
    {

    }
}
于 2011-08-02T09:19:41.873 回答