简体   繁体   English

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

[英]CMTime in AVPlayer's addPeriodicTimeObserverForInterval: callback never reaches item's duration

I use AVPlayer's -(id)addPeriodicTimeObserverForInterval: queue: usingBlock: method to update UI up to playback progress. 我使用AVPlayer的-(id)addPeriodicTimeObserverForInterval: queue: usingBlock:方法将UI更新到播放进度。 However, my progress bar never reaches end. 但是,我的进度条永远不会结束。

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);

                                                }];

logs: 日志:

App[2373:792179] duration: 3968.00

hit play button 点击播放按钮

App[2373:792179] progress 0011.176
App[2373:792179] progress 0021.175
...
App[2373:792179] progress 3701.319
App[2373:792179] progress 3704.000

Should not I expect last number to be 3968.0 ? 我不应该期望最后一个数字是3968.0吗?

Audio is streamed from server. 音频从服务器流式传输。

EDIT 编辑

Last progress number is ALWAYS duration - 0.264 sec whatever actual duration length is. 最后进度数总是 duration - 0.264 sec无论实际持续时间长度是duration - 0.264 sec

This is so strange, I wish we could use emoticons on SO. 这太奇怪了,我希望我们可以在SO上使用表情符号。

Good question. 好问题。 Try using CMTimeGetSeconds(time) instead of calculating the total seconds yourself. 尝试使用CMTimeGetSeconds(time)而不是自己计算总秒数。

Additionally, try to use CMTimeMakeWithSeconds(10.0, NSEC_PER_SEC) to create the time for the periodic time observer. 此外,尝试使用CMTimeMakeWithSeconds(10.0, NSEC_PER_SEC)为周期性时间观察器创建时间。

This post helped me a lot to wrap my head around the enigma that is CMTime: https://warrenmoore.net/understanding-cmtime 这篇文章帮助我了解了很多关于CMTime的谜团: https ://warrenmoore.net/understanding-cmtime

The docs clearly state: 文档明确指出:

You must retain the returned value as long as you want the time observer to be invoked by the player. 只要您希望播放器调用时间观察器,您就必须保留返回的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM