为什么iOS中的avplayer时间不正确

我的公司需要一个视频应用,所以我昨天开始工作。

你可以看到这个avplayer的代码:

// to get palying time
-(void)addProgressObserver{
    __weak typeof(self) weakSelf = self ;
    //这里设置每秒执行一次
    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        double current=weakSelf.player.currentItem.duration.value/weakSelf.player.currentItem.duration.timescale*1.0;
        weakSelf.playerProgressDuration = current ;
    }];
}

// to get total time
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    AVPlayerItem *playerItem=object;
    if ([keyPath isEqualToString:@"status"]) {
        AVPlayerStatus status= [[change objectForKey:@"new"] intValue];
        if(status==AVPlayerStatusReadyToPlay){
            self.playerTotalDuration = playerItem.duration.value / playerItem.duration.timescale * 1.0 ;
        }
    }
}

你可以看到这段代码。 方法'addProgressObserver'可以获得播放时间:

weakSelf.playerProgressDuration = current ;

KVO方法可以监听属性(属性的名称为'status'),并获取视频的总时间:

self.playerTotalDuration = playerItem.duration.value  / playerItem.duration.timescale * 1.0 ;

但是,这个代码并运行ipad 3(ios 8.1),我可以找到一个问题:

视频播放已完成,但self.playerProgressDuration不能等于self.playerTotalDuration!

所以我添加了一个通知:AVPlayerItemDidPlayToEndTimeNotification,但通知无法执行!

问题:1,视频播放完成后,为什么self.playerProgressDuration不能等于self.playerTotalDuration? 2,视频播放完成,为什么不执行通知?

请帮帮我,谢谢。

author: user5465320
采纳答案:

答:1,请使用double或int_64; 2,因为通知的对象存在错误! 请将nil或AVPlayerItem设置为通知对象,不能将AVPlayer设置为通知对象!

author: user5465320

参考更多解答: Why the avplayer time is incorrect in iOS ,转载请保留出处为什么iOS中的avplayer时间不正确及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:ios