当 AVQueuePlayer 完成最后一个 playeritem 时如何做某事 [英] How to do something when AVQueuePlayer finishes the last playeritem
问题描述
我有一个 AVQueuePlayer,我正在从 4 个 AVPlayerItems 的数组中创建它,并且一切正常.
I've got an AVQueuePlayer which I'm creating from an array of 4 AVPlayerItems, and it all plays fine.
我想在队列中的最后一项播放完毕后做一些事情,我在这里查看了大量答案,这是我想要的最有希望的答案:执行代码的最佳方式声音播放完毕
I want to do something when the last item in the queue finishes playing, I've looked a load of answers on here and this is the one that looks most promising for what I want: The best way to execute code AFTER a sound has finished playing
在我的按钮处理程序中,我有这个代码:
In my button handler i have this code:
static const NSString *ItemStatusContext;
[thePlayerItemA addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd)
name:AVPlayerItemDidPlayToEndTimeNotification
object:thePlayerItemA];
theQueuePlayer = [AVQueuePlayer playerWithPlayerItem:thePlayerItemA];
[theQueuePlayer play];
然后我有一个函数来处理 playerItemDidReachEnd:
and then I have a function to handle playerItemDidReachEnd:
- (void)playerItemDidReachEnd:(NSNotification *)notification {
// Do stuff here
NSLog(@"IT REACHED THE END");
}
但是当我运行这个时,我得到一个内部不一致异常:
But when I run this I get an Internal Inconsistency Exception:
An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: status
Observed object: <AVPlayerItem: 0x735a130, asset = <AVURLAsset: 0x73559c0, URL = file://localhost/Users/mike/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/A0DBEC13-2DA6-4887-B29D-B43A78E173B8/Phonics%2001.app/yes.mp3>>
Change: {
kind = 1;
}
我做错了什么?
推荐答案
这种方法似乎对我有用,在我的按钮处理程序中,我有很多东西可以为我想播放的 4 个 mp3 文件创建 URL,然后:
This approach seems to work for me, within my button handler i've got a bunch of stuff to create URLs for the 4 mp3 files I want to play, then:
AVPlayerItem *thePlayerItemA = [[AVPlayerItem alloc] initWithURL:urlA];
AVPlayerItem *thePlayerItemB = [[AVPlayerItem alloc] initWithURL:urlB];
AVPlayerItem *thePlayerItemC = [[AVPlayerItem alloc] initWithURL:urlC];
AVPlayerItem *thePlayerItemD = [[AVPlayerItem alloc] initWithURL:urlD];
NSArray *theItems = [NSArray arrayWithObjects:thePlayerItemA, thePlayerItemB, thePlayerItemC, thePlayerItemD, nil];
theQueuePlayer = [AVQueuePlayer queuePlayerWithItems:theItems];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[theItems lastObject]];
[theQueuePlayer play];
之后我实现了playerItemDidReachEnd"选择器,如下所示:
After which I have implement the 'playerItemDidReachEnd' selector like this:
- (void)playerItemDidReachEnd:(NSNotification *)notification {
// Do stuff here
NSLog(@"IT REACHED THE END");
}
这会将 4 个 MP3 文件排入队列,然后当最后一段音频结束时,它会调用选择器,并且我的消息会出现在控制台中.
This queues up the 4 MP3 files and then when the last piece of audio has finished it calls the selector and my message appears in the console.
我希望这对其他人有用.
I hope that this is useful for someone else.
这篇关于当 AVQueuePlayer 完成最后一个 playeritem 时如何做某事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!