我有一个使用AVQueuePlayer
播放音乐的TvOS应用程序(歌曲存储在远程服务器上)。通常,歌曲连续播放几个小时。有时候,它会在很短的时间内滞后并继续玩。但有些时候,它只是持续滞后。当发生这种情况时,重新启动应用程序或重新启动Apple TV无法解决问题。
我的应用程序没有滞后(只播放播放器),Apple TV上的互联网连接完美(使用SpeedTest检查)并且远程服务器正常响应(我的电脑没有问题缓冲文件和ping非常快。)
因此,我已在rate
的{{1}}属性上添加了观察者,以检查其AVQueuePlayer
的某些属性,以获取有关该问题的更多信息。
currentItem
这会产生以下日志:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"rate"] && object == queuePlayer) {
NSLog(@"rate: %@", @(queuePlayer.rate));
NSLog(@"duration: %@", @(CMTimeGetSeconds(queuePlayer.currentItem.duration)));
NSLog(@"time: %@", @(CMTimeGetSeconds(queuePlayer.currentItem.currentTime)));
NSLog(@"playbackLikelyToKeepUp: %@", @(queuePlayer.currentItem.playbackLikelyToKeepUp));
NSLog(@"playbackBufferEmpty: %@", @(queuePlayer.currentItem.playbackBufferEmpty));
NSLog(@"playbackBufferFull: %@", @(queuePlayer.currentItem.playbackBufferFull));
NSLog(@"bufferSize: %@", @([self bufferSize]));
double currentTime = CMTimeGetSeconds(queuePlayer.currentItem.currentTime);
if(queuePlayer.rate == 0 && currentTime != 0 && currentTime < CMTimeGetSeconds(queuePlayer.currentItem.duration) - 0.1) {
[queuePlayer prerollAtRate:1 completionHandler:^(BOOL finished) {
NSLog(@"Finished prerolling");
[queuePlayer play];
}];
}
}
}
- (NSTimeInterval) bufferSize {
NSArray *loadedTimeRanges = [[queuePlayer currentItem] loadedTimeRanges];
CMTimeRange timeRange = [[loadedTimeRanges objectAtIndex:0] CMTimeRangeValue];
Float64 startSeconds = CMTimeGetSeconds(timeRange.start);
Float64 durationSeconds = CMTimeGetSeconds(timeRange.duration);
NSTimeInterval result = startSeconds + durationSeconds;
return result;
}
正如您所看到的,2016-04-13 15:59:49.947 Virtual Juke[517:427012] rate: 0
2016-04-13 15:59:49.949 Virtual Juke[517:427012] duration: 203.5461224489796
2016-04-13 15:59:49.949 Virtual Juke[517:427012] time: 29.325428829
2016-04-13 15:59:49.950 Virtual Juke[517:427012] playbackLikelyToKeepUp: 1
2016-04-13 15:59:49.950 Virtual Juke[517:427012] playbackBufferEmpty: 1
2016-04-13 15:59:49.950 Virtual Juke[517:427012] playbackBufferFull: 0
2016-04-13 15:59:49.953 Virtual Juke[517:427012] bufferSize: 203.52
2016-04-13 15:59:49.958 Virtual Juke[517:427012] Finished prerolling
2016-04-13 15:59:49.959 Virtual Juke[517:427012] rate: 1
2016-04-13 15:59:50.733 Virtual Juke[517:427012] duration: 203.5461224489796
2016-04-13 15:59:50.733 Virtual Juke[517:427012] time: 28.566945193
2016-04-13 15:59:50.734 Virtual Juke[517:427012] playbackLikelyToKeepUp: 1
2016-04-13 15:59:50.734 Virtual Juke[517:427012] playbackBufferEmpty: 1
2016-04-13 15:59:50.734 Virtual Juke[517:427012] playbackBufferFull: 0
2016-04-13 15:59:50.737 Virtual Juke[517:427012] bufferSize: 203.52
有一些矛盾的属性值。它说它的缓冲区在暂停时是空的,当它恢复时,它已经准备好了...当我计算缓冲区时,看起来它已经缓冲了整首歌。
究竟是什么问题,为什么会发生?