AVPlayer添加播放进度监听

Posted 博BOBO

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AVPlayer添加播放进度监听相关的知识,希望对你有一定的参考价值。

其实,苹果已经提供过有关的API:和AVPlayer进度有关API

/// 添加监听.以及回调
__weak typeof(self) weakSelf = self;
[_player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) 
	/// 更新播放进度
	[weakSelf updateProgress];
];
self.audioPlayer.addPeriodicTimeObserver(forInterval: CMTime.init(value: CMTimeValue(1.0), timescale: CMTimeScale(1.0)), queue: DispatchQueue.main) 
            [unowned self] (time)  in
            let currt = CMTimeGetSeconds(time)
            if Int(currt) + 1 == Int(self.audio_Model.currentDuration)
                return
            
            self.audio_Model.currentDuration = currt
            self.pro = currt/self.audio_Model.audioDuration
            self.upTimeAction(haveType: Int(currt) == Int(self.audio_Model.audioDuration))
        

这个传入的参数:
CMTime就是你希望多久回调一次.
queue类似于没记错的话是下载框架中的queue.就是你希望在哪个线程回调下面的block.
block参数.CMTime time.
这个是当前播放进度.和player.currentTime.或者是player.currentItem.currentTime一致.

获取总播放时长用player.currentItem.asset.duration就行

以上是关于AVPlayer添加播放进度监听的主要内容,如果未能解决你的问题,请参考以下文章