繁体   English   中英

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

[英]Why the avplayer time is incorrect in iOS

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

您可以看到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,视频播放完毕,为什么不执行通知?

请帮助我,谢谢。

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

暂无
暂无

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

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