AVPlayer的addPeriodicTimeObserverForInterval中的CMTime:回调永远不会达到项目的持续时间



我使用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无论实际持续时间长度是什么。

这太奇怪了,我希望我们可以在SO上使用表情符号。

好问题。尝试使用CMTimeGetSeconds(time)而不是自己计算总数。

此外,尝试使用CMTimeMakeWithSeconds(10.0, NSEC_PER_SEC)为定期时间观察者创建时间。

这篇文章帮助我将头缠绕在CMTime的谜团中:

文档清楚地表明:

,只要您希望播放器调用时间观察者,就必须保留返回的值。

最新更新