Ios 在AVQueuePlayer中检测当前项目并知道它何时更改?
我现在有一个AVQUEUE播放器,按顺序播放一些.aif文件。在播放每个音频时,我希望执行与特定声音相关的操作(例如更改imageview的uiimage) 我的问题是正确设置NSNotification。我已将其设置为在队列完成最后一个对象时通知我,但我在接收每个当前项的通知时遇到问题。以下是我所拥有的:Ios 在AVQueuePlayer中检测当前项目并知道它何时更改?,ios,avaudioplayer,avplayer,nsnotifications,avqueueplayer,Ios,Avaudioplayer,Avplayer,Nsnotifications,Avqueueplayer,我现在有一个AVQUEUE播放器,按顺序播放一些.aif文件。在播放每个音频时,我希望执行与特定声音相关的操作(例如更改imageview的uiimage) 我的问题是正确设置NSNotification。我已将其设置为在队列完成最后一个对象时通知我,但我在接收每个当前项的通知时遇到问题。以下是我所拥有的: //Setting Up My Queue List yellowVoice = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWi
//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];
}
}
Bhushan的答案缺少触发每个项目通知的循环,因此:
for (AVPlayerItem *playerItem in queuePlayer.items) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentItemIs:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
}
任何人我真的很感激。
for (AVPlayerItem *playerItem in queuePlayer.items) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentItemIs:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
}