gpt4 book ai didi

ios - AVQueuePlayer AVPlayerItemDidPlayToEndTimeNotification 调用失败

转载 作者:行者123 更新时间:2023-11-29 03:24:41 24 4
gpt4 key购买 nike

我使用 AVQueuePlayer 循环遍历 AVPlayerItems 数组。我循环它的方式是听 AVPlayerItemDidPlayToEndTimeNotification,每次调用它时,我都会将当前对象添加到队列的末尾。代码如下:

    viewWillAppear
{
...

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_queuePlayer currentItem]];

[_queuePlayer play];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
AVPlayerItem *fakeNew = [[AVPlayerItem alloc] initWithAsset:p.asset];
if (_queuePlayer.items.count == 1)
{
[p seekToTime:kCMTimeZero];
[_queuePlayer play];
}
else
{
[_queuePlayer insertItem:fakeNew afterItem:[_queuePlayer.items lastObject]];
}
NSLog(@"array of items to play:%lu", (unsigned long)_queuePlayer.items.count);

}

问题是,该方法仅针对播放的第一个视频调用,之后,该方法停止被调用,因此,例如,如果我在数组中有 2 部电影,它将同时播放它们+第一个再次,知道为什么会发生这种情况吗?

更多信息:还尝试每次都制作一个新的播放器并将其设置为分层。多次发送通知失败都一样

- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[self.playList removeObjectAtIndex:0];
[self.playList addObject:p];
AVPlayer *newPlayer = [[AVPlayer alloc] initWithPlayerItem:[self.playList objectAtIndex:0]];
_player = newPlayer;
self.AVPlayerLayerView.layer.player = self.player;
[_player play];

}

最佳答案

经过一番困惑之后,显然出于某种原因, View 每次都未注册为观察者,我只是在每次通知后删除并添加了观察者:

- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
AVPlayerItem *fakeNewItem = [[AVPlayerItem alloc] initWithAsset:p.asset];
[self.playList removeObjectAtIndex:0];
[self.playList addObject:fakeNewItem];
AVPlayer *newPlayer = [[AVPlayer alloc] initWithPlayerItem:[self.playList objectAtIndex:0]];
_player = newPlayer;
self.AVPlayerLayerView.layer.player = self.player;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_player currentItem]];
[_player play];

}

关于ios - AVQueuePlayer AVPlayerItemDidPlayToEndTimeNotification 调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20611371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com