在AVQueuePlayer中检测当前项目并知道它何时更改? [英] Detecting Current Item in AVQueuePlayer and Knowing when it Changes?

查看:90
本文介绍了在AVQueuePlayer中检测当前项目并知道它何时更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个AVQueuePlayer依次播放几个.aif文件.当它遍历每个音频时,我想执行与该特定声音相关的操作(例如,更改imageview的uiimage).

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).

我的问题是正确设置NSNotification.我已将其设置为在队列中最后一个对象结束时通知我,但是我无法接收到当前每个项目的通知.这是我所拥有的:

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];

我觉得自己被name参数卡住了.我应该将其更改为什么?以及当切换时,我如何找出当前正在播放的播放器项目的名称?谢谢!

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];


    }


}

推荐答案

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

这篇关于在AVQueuePlayer中检测当前项目并知道它何时更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆