简体   繁体   English

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

[英]Why the avplayer time is incorrect in iOS

My company need a video app , so i start working yesterday . 我的公司需要一个视频应用程序,所以我昨天开始工作。

You can see this code of 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 ;
        }
    }
}

You can see this code . 您可以看到此代码。 the method 'addProgressObserver' can get playing time : 方法“ addProgressObserver”可以获取播放时间:

weakSelf.playerProgressDuration = current ;

the KVO method can listen a property (the property's name is 'status') , and get the video's total time : KVO方法可以侦听某个属性(该属性的名称为'status'),并获取视频的总时间:

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

but , bulid this code and running ipad 3 (ios 8.1) , I can find a issue : 但是,建立这段代码并运行ipad 3(ios 8.1),我可以发现一个问题:

Video playback is completed , but the self.playerProgressDuration cannot equal to self.playerTotalDuration ! 视频播放已完成,但是self.playerProgressDuration不能等于self.playerTotalDuration!

So I add a notification : AVPlayerItemDidPlayToEndTimeNotification , but the notification can not be executed ! 所以我添加了一个通知:AVPlayerItemDidPlayToEndTimeNotification,但是该通知无法执行!

Question : 1、Video playback is completed ,why the self.playerProgressDuration cannot equal to self.playerTotalDuration ? 问题:1,视频播放完毕,为什么self.playerProgressDuration不能等于self.playerTotalDuration? 2、Video playback is completed ,Why the notification does not be executed ? 2,视频播放完毕,为什么不执行通知?

Please help me , Thanks. 请帮助我,谢谢。

Answer : 1、please use double or int_64 ; 答:1,请使用double或int_64; 2、because the notification's object exist error ! 2,由于通知对象存在错误! please set nil or AVPlayerItem to notification's object , can not set AVPlayer to notification's object ! 请将nil或AVPlayerItem设置为通知对象,不能将AVPlayer设置为通知对象!

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

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