AVPlayer的addPeriodicTimeObserverForInterval:回调中的CMTime从未达到项目的持续时间

3
我使用AVPlayer的-(id)addPeriodicTimeObserverForInterval: queue: usingBlock:方法来更新播放进度,但是我的进度条从未到达终点。
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秒

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

2个回答

0

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

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

这篇文章帮助我很多,让我理解了CMTime的奥秘:https://warrenmoore.net/understanding-cmtime


相同的东西(另一段声音):duration:4.736 maxProgress:4.472。可能的原因是什么?编辑:刚看到您更新了答案,我会尝试其他选项。 - kerd
@kerd 非常有趣。你能否使用CMTimeGetSeconds打印持续时间?还有一件事:尝试更精细的回调值。不要使用10秒,而是尝试1.0/30.0 - Johannes Fahrenkrug
在第一个注释中,我写了 CMTimeGetSeconds 的结果。如果不清楚,请见谅。此外,我尝试过的 CMTimeMakeWithSeconds(10.0, NSEC_PER_SEC) 是最精细的时间单位 - 它以 10 纳秒为单位,而我在原始帖子中使用的是 0.01 秒。 - kerd
尝试使用CMTimeMakeWithSeconds(1.0/30.0, NSEC_PER_SEC)。在您的第一个评论中,maxProgress低于持续时间。但在问题中,它比持续时间更高。因此,回调可能只是因为粒度不够细而没有触发。 - Johannes Fahrenkrug
@kerd 实际上,检查 CMTimeGetSeconds(self.player.currentTime) 比使用传入的 time 更准确,因为它实际上是块被调用时的时间。 - Johannes Fahrenkrug
对不起,返回的是相同的结果。 - kerd

0
文档明确说明:

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


抱歉,您能否协助进一步澄清这些信息如何有所帮助?顺便问一下,观察者已经被调用了吗? - kerd
他说你需要持有观察者对象的强引用,否则它会被释放。例如,你的UIViewController可以有一个名为playerTimeObserver的属性。 - user3344977

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接