我在 AVQueuePlayer 上玩得很开心。很简单,我正在使用 playerItemWithURL: 构建的 AVPlayerItems 数组上制作它,并指向网络上服务器上的视频资产。
如果我尝试在刺激器(原文如此)中运行这个东西,它会播放第一个资产,开始播放第二个资产,然后就死了。有时它甚至没有通过第一个资产。
现在,我知道 sim 的行为与设备不同,所以我也在设备上尝试了它,但遇到了同样的问题。
当我记录项目的各种状态变化时,我看到缓冲区用完了,播放器就死了。对于正在播放项目队列的构造,这对我来说似乎有点愚蠢......当然缓冲区可能会不足,但为什么它会死?
我做错了什么,或者这是它应该如何表现?
我通过观察loadedTimeRanges 属性以及当它改变时向播放器发送PLAY 来解决这个问题。但这会导致播放卡顿,这也很糟糕。
这是我的代码。有人可以告诉我如何在没有播放完全废话的情况下做到这一点吗?
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePlayerItemReachedEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.queuePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePlayerStalled:) name:AVPlayerItemPlaybackStalledNotification object:self.queuePlayer];
NSString *baseURL = @"http://www.aDomain.com/assets/";
NSMutableArray *vidItemArray = [[NSMutableArray alloc] initWithCapacity:5];
for (int i = 1; i <= 5; i++) {
AVPlayerItem *vid = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:[baseURL stringByAppendingFormat:@"%d.mov", i]]];
[vid addObserver:self forKeyPath:@"status" options:0 context:nil];
[vid addObserver:self forKeyPath:@"playbackBufferEmpty" options:0 context:nil];
[vid addObserver:self forKeyPath:@"loadedTimeRanges" options:0 context:nil];
[vidItemArray addObject:vid];
}
self.queuePlayer = [AVQueuePlayer queuePlayerWithItems:vidItemArray];
[self.mPlaybackView setPlayer:self.queuePlayer];
}
- (void)handlePlayerItemReachedEnd:(NSNotification *)notification {
NSLog(@"Clip #%d finished playing", [self.queuePlayer.items indexOfObject:self.queuePlayer.currentItem]);
}
- (void)handlePlayerStalled:(NSNotification *)notification {
NSLog(@"CLIP STALLED...");
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([object isKindOfClass:[AVPlayerItem class]]) {
AVPlayerItem *item = (AVPlayerItem *)object;
if ([keyPath isEqualToString:@"status"]) {
switch (item.status) {
case AVPlayerItemStatusFailed:
NSLog(@"player item status failed");
break;
case AVPlayerItemStatusReadyToPlay:
NSLog(@"player item status is ready to play");
[self.queuePlayer play];
break;
case AVPlayerItemStatusUnknown:
NSLog(@"player item status is unknown");
break;
}
}
else if ([keyPath isEqualToString:@"playbackBufferEmpty"]) {
if (item.playbackBufferEmpty) {
NSLog(@"player item playback buffer is empty");
}
}
else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
NSLog(@"Loaded time range = %@", item.loadedTimeRanges);
self.queuePlayer play];
}
}
}