addPeriodicTimeObserverForInterval称为额外时间 [英] addPeriodicTimeObserverForInterval called extra time

查看:240
本文介绍了addPeriodicTimeObserverForInterval称为额外时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AVPlayer,具有4秒的视频(NSTimeInterval duration = CMTimeGetSeconds(self.playerItem.asset.duration) = 4).

I have a AVPlayer with 4 sec video (NSTimeInterval duration = CMTimeGetSeconds(self.playerItem.asset.duration) = 4).

我想通过更改来更新用户界面:

I'd like to update UI with changes:

self.periodicTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {

    [weakSelf currentTimeDidChange:CMTimeGetSeconds(time)];

}];

但是由于某些原因,我得到了更多的UI调用:

But for some reasons I get extra calls to UI:

- (void)currentTimeDidChange:(NSTimeInterval)currentTime {
    NSLog(@"timePassed %f, total: %f", currentTime, self.duration);
}

日志:

2015-07-23 13:47:07.412   timePassed 0.000000, total: 4.000000
2015-07-23 13:47:07.448   timePassed 0.002814, total: 4.000000
2015-07-23 13:47:07.450   timePassed 0.005481, total: 4.000000
2015-07-23 13:47:08.447   timePassed 1.001473, total: 4.000000
2015-07-23 13:47:09.446   timePassed 2.001612, total: 4.000000
2015-07-23 13:47:10.446   timePassed 3.002021, total: 4.000000
2015-07-23 13:47:11.446   timePassed 4.002139, total: 4.000000
2015-07-23 13:47:12.445   timePassed 5.001977, total: 4.000000
2015-07-23 13:47:12.492   timePassed 5.046618, total: 4.000000

感谢您的帮助

推荐答案

您需要确保您没有多次调用periodTimeObserver,因此请编写此代码

you need to be sure that you are not calling periodicTimeObserver many time so do this write this code

if (self.periodicTimeObserver == nil){
self.periodicTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {

    [weakSelf currentTimeDidChange:CMTimeGetSeconds(time)];

}];
}

然后,在您的视频播放完毕后,立即移除观察者

then as soon as your video is finishes playing remove observer

[player removeTimeObserver:self.periodicTimeObserver];
self.periodicTimeObserver = nil;

这篇关于addPeriodicTimeObserverForInterval称为额外时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆