我有一个 AVQueuePlayer,它是我从 4 个 AVPlayerItem 的数组创建的,并且一切正常。
我想在队列中的最后一个项目播放完后做点什么,我在这里查看了很多答案,这是看起来最有希望实现我想要的答案: The best way to execute code AFTER a sound has finished playing
在我的按钮处理程序中我有这段代码:
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:
- (void)playerItemDidReachEnd:(NSNotification *)notification {
// Do stuff here
NSLog(@"IT REACHED THE END");
}
但是当我运行它时,我得到一个内部不一致异常:
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,然后:
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”选择器:
- (void)playerItemDidReachEnd:(NSNotification *)notification {
// Do stuff here
NSLog(@"IT REACHED THE END");
}
这会将 4 个 MP3 文件排队,然后当最后一段音频播放完毕时,它会调用选择器,我的消息会出现在控制台中。
我希望这对其他人有用。
关于objective-c - 当 AVQueuePlayer 完成最后一个播放器项目时如何做某事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12187686/