在AVQueuePlayer中检测当前项目并知道它何时更改?
我目前有一个AVQueuePlayer按顺序播放几个.aif文件。 当它通过每个audio时,我想要执行与特定声音相关的操作(例如,更改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];
我觉得我卡在名称参数。 我应该改变什么? 我怎样才能find当前正在播放的播放器的名称,因为它切换? 谢谢!
//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 } }
Bhushan的答案是缺less循环触发每个项目的通知,所以:
for (AVPlayerItem *playerItem in queuePlayer.items) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentItemIs:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem]; }
- iOS – 试图播放MP3文件失败
- 应用程序:didReceiveRemoteNotification:fetchCompletionHandler:调用时不在UIApplicateStateBackground中
- AVAudioPlayer错误加载文件
- 如何在听音乐的同时快速使用AVAudioplayer?
- 在iOS中播放encryption的mp3
- AVAudioPlayer帮助:同时播放多个声音,一次停止所有声音,以及解决自动参考计数问题
- MPMediaItem不使用MPMediaItemPropertyAssetURL在AVAudioPlayer中播放
- 触发AVAudioPlayer停止所有的声音和播放select,除非单击播放(选定)button
- 设备locking时从iPod库中播放AVAudio