我使用AVPlayer的-(id)addPeriodicTimeObserverForInterval: queue: usingBlock:方法来更新UI直到播放进度。然而,我的进度条永远不会到达终点。
CMTime duration = self.player.currentItem.asset.duration;
float totalSeconds = (Float64)(duration.value * 1000) / (Float64)(duration.timescale);
NSLog(@"duration: %.2f", totalSeconds);
__weak __typeof(self) welf = self;
_mTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(10, 1000)
queue:NULL // main queue
usingBlock:^(CMTime time) {
float totalSeconds = (Float64)(time.value * 1000) / (Float64)(time.timescale);
NSLog(@"progress %f", totalSeconds);
}];日志:
App[2373:792179] duration: 3968.00按下播放按钮
App[2373:792179] progress 0011.176
App[2373:792179] progress 0021.175
...
App[2373:792179] progress 3701.319
App[2373:792179] progress 3704.000我不应该期望最后一个数字是3968.0吗?
音频是从服务器上传输的。
编辑
最后一个进度号总是duration - 0.264 sec,不管实际持续时间长度是多少。
这太奇怪了,我真希望我们能在上面用表情符号。
发布于 2016-06-22 19:20:40
问得好。尝试使用CMTimeGetSeconds(time),而不是自己计算总秒。
另外,尝试使用CMTimeMakeWithSeconds(10.0, NSEC_PER_SEC)来创建周期性时间观察者的时间。
这篇文章帮助我了解了CMTime:https://warrenmoore.net/understanding-cmtime这个谜。
发布于 2016-06-22 23:56:56
这些文档明确指出:
只要希望播放机调用时间观察者,就必须保留返回的值。
https://stackoverflow.com/questions/37976453
复制相似问题