ios - 检测 AVQueuePlayer 中的当前项并知道它何时更改?

标签 ios avaudioplayer avplayer nsnotifications avqueueplayer

我目前有一个 AVQueuePlayer 按顺序播放几个 .aif 文件。当它通过每个音频时,我想执行与该特定声音相关的操作(例如更改 ImageView 的 uiimage)。

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

//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 参数卡住了。我应该把它改成什么?我如何在切换时找出当前正在播放的播放器项目的名称?谢谢!

//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
   }
}

关于ios - 检测 AVQueuePlayer 中的当前项并知道它何时更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15346127/

相关文章:

iOS WKWebview : Always allow camera permission

ios - 将声音输出到接收器而不是扬声器

iOS:AVPlayer play 有时无法播放

ios - 日期(timeIntervalSince1970 :) returns 2 different results

ios - 如何实现 CoreData 记录的重新排序?

iphone - 如何在iPhone中使用图像路径和音频路径播放带有图像的歌曲

ios - 在 AVPlayerViewController 中点击播放按钮时执行操作

ios - 通知控制中心同时播放多个音频

ios - 超过 1 个 UIPickerView Swift

iphone - 在 IOS 中录制和播放需要不同的 AVAudioSession