Detecting Current Item in AVQueuePlayer and Knowing when it Changes?

岁酱吖の 提交于 2019-12-12 13:50:10

问题


I currently have an AVQueuePlayer playing a few .aif files in sequence. As it goes through each audio I want to perform an action related to that specific sound (for example changing an imageview's uiimage).

My problem is setting the NSNotification correctly. I've set it to notify me when the queue finished with the last object, but I'm having trouble receiving an a notification for each current item. Here is what I have:

//Setting Up My Queue List
    yellowVoice = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/YellowVoice.aif", [[NSBundle mainBundle] resourcePath]]]];
    orangeVoice = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/OrangeVoice.aif", [[NSBundle mainBundle] resourcePath]]]];
    redVoice = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/RedVoice.aif", [[NSBundle mainBundle] resourcePath]]]];
    pinkVoice = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/PinkVoice.aif", [[NSBundle mainBundle] resourcePath]]]];

    NSArray *soundEmotions = [NSArray arrayWithObjects:yellowVoice, orangeVoice, redVoice, pinkVoice, nil];

    soundQueue = [AVQueuePlayer queuePlayerWithItems:soundEmotions];

// Notify me when queue reaches the end of the last player item
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[soundEmotions lastObject]];

// Notify me when a new player item begins and which one it is
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(currentItemIs:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[soundQueue currentItem]];

[soundQueue play];

I feel like I'm stuck with the name parameter. What should I change it to? And how would I find out the name of the player item that is currently playing, as it switches? Thanks!

//Here is one of the things I want to do with that info
- (void)currentItemIs:(NSNotification *)notification {

    if (soundQueue.currentItem ==yellowVoice) {

        UIImage * yellowImage = [UIImage imageNamed:@"yellow.png"];
        [UIView transitionWithView:self.view
                          duration:1.0f
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                            mainImage.image = yellowImage;
                        } completion:NULL];


    }


    if (soundQueue.currentItem ==yellowVoice) {

        UIImage * orangeImage = [UIImage imageNamed:@"orange.png"];
        [UIView transitionWithView:self.view
                          duration:1.0f
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                            mainImage.image = orangeImage;
                        } completion:NULL];


    }


}

回答1:


- (void)currentItemIs:(NSNotification *)notification 
{
   AVPlayerItem *p = [notification object];
   if (p ==yellowVoice) {
   //rest of the code
   }
}



回答2:


Bhushan's answer is missing the loop to trigger the notification for each item, so:

for (AVPlayerItem *playerItem in queuePlayer.items) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentItemIs:)  name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
        }


来源:https://stackoverflow.com/questions/15346127/detecting-current-item-in-avqueueplayer-and-knowing-when-it-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!