开发者

NSNotificationCenter with arguments

开发者 https://www.devze.com 2023-03-25 10:30 出处:网络
I am implementing an Audio based application. In that I am playing two different sounds using two AVPlayers. I need to do different actions once the sounds played. For this I used NSNotifications. But

I am implementing an Audio based application. In that I am playing two different sounds using two AVPlayers. I need to do different actions once the sounds played. For this I used NSNotifications. But my problem is I am not able to find the Notifications related to which player. My notifications code and selector code is as follows please any one suggest me what the mistake I did.

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

}

Thanks in advance, Chandra.


I need to change the code base as follows,

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

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

And selector code should be as follows,

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

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

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

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消