Ios AVPlayer中的CMTime';s addPeriodicTimeObserverForInterval:回调永远不会到达项';持续时间
我使用AVPlayer的Ios AVPlayer中的CMTime';s addPeriodicTimeObserverForInterval:回调永远不会到达项';持续时间,ios,objective-c,avplayer,avplayeritem,cmtime,Ios,Objective C,Avplayer,Avplayeritem,Cmtime,我使用AVPlayer的-(id)addPeriodicTimeObserverForInterval:queue:usingBlock:方法更新UI,直到播放进度。然而,我的进度条永远不会结束 CMTime duration = self.player.currentItem.asset.duration; float totalSeconds = (Float64)(duration.value * 1000) / (Float64)(duration.timescale); NSLog(@
-(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);
}];
日志:
点击播放按钮
我不应该期望最后一个数字是3968.0
音频从服务器流式传输
编辑
最后一个进度编号始终为持续时间-0.264秒,无论实际持续时间长度是多少
这太奇怪了,我希望我们能在上面使用表情符号。好问题。尝试使用
CMTimeGetSeconds(time)
而不是自己计算总秒数
此外,尝试使用CMTimeMakeWithSeconds(10.0,纳秒/秒)
为周期时间观察者创建时间
这篇文章帮助我解开了CMTime的谜团:文档明确指出: 只要希望玩家调用时间观察者,就必须保留返回值
相同的内容(其他声音):
持续时间:4.736最大进度:4.472
。原因可能是什么?编辑:刚刚看到你更新的答案,我会试试其他的options@kerd那很有趣。您是否也可以使用CMTimeGetSeconds
打印持续时间?还有一件事:尝试更细粒度的回调值。尝试1.0/30.0
,而不是10秒。在第一条评论中,我写了CMTimeGetSeconds
的结果。对不起,如果不清楚的话。另外,我试过的CMTimeMakeWithSeconds(10.0,NSEC\u/SEC)
是最细的颗粒-它花费了10纳秒,而不是我在原始帖子中使用的0.01秒。试着CMTimeMakeWithSeconds(1.0/30.0,NSEC\u/SEC)
。在您的第一条评论中,maxProgress低于持续时间。在这个问题上是高于持续时间的。所以可能是回调没有触发,因为它不够细粒度。@kerd实际上检查CMTimeGetSeconds(self.player.currentTime)
比使用传入的time
更准确,因为那实际上是调用块的时间。对不起,但是,您能否帮助进一步了解这些信息有何帮助?顺便说一句,观测者不是已经被调用了吗?他说你需要保持对观测者对象的强引用,否则它将被释放。例如,UIViewController可以有一个名为playerTimeObserver的属性。
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