当 AVQueuePlayer 完成最后一个播放器项目时如何执行某些操作

2024-04-16

我有一个 AVQueuePlayer,它是由 4 个 AVPlayerItems 组成的数组创建的,并且一切都运行良好。

当队列中的最后一项完成播放时,我想做点什么,我在这里查看了很多答案,这是看起来最有希望实现我想要的答案:声音播放完毕后执行代码的最佳方式 https://stackoverflow.com/questions/10604740/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 文件排队,然后当最后一段音频完成时,它会调用选择器,并且我的消息会出现在控制台中。

我希望这对其他人有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当 AVQueuePlayer 完成最后一个播放器项目时如何执行某些操作 的相关文章

随机推荐