我有一个4秒视频的AVPlayer(NSTimeInterval duration = CMTimeGetSeconds(self.playerItem.asset.duration)
= 4)。
我想通过更改更新用户界面:
self.periodicTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
[weakSelf currentTimeDidChange:CMTimeGetSeconds(time)];
}];
但由于某些原因,我得到额外的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
感谢任何帮助
答案 0 :(得分:4)
你需要确保你没有多次调用periodicTimeObserver所以这样写这段代码
if (self.periodicTimeObserver == nil){
self.periodicTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
[weakSelf currentTimeDidChange:CMTimeGetSeconds(time)];
}];
}
然后,一旦您的视频播放完毕,请删除观察者
[player removeTimeObserver:self.periodicTimeObserver];
self.periodicTimeObserver = nil;
答案 1 :(得分:0)
我也有不一致的地方。所以我不是依赖于块给出的值,而是依赖于playerItem本身的值:
self.periodicTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:nil usingBlock:^(__unused CMTime time) {
[weakSelf currentTimeDidChange:CMTimeGetSeconds(self.playerItem.currentTime)];
}];