AVPlayer查找后的当前时间不正确


func seekFullRead(seconds: Float64, completion: (() -> ())? = nil) {
let targetTime:CMTime = CMTimeMakeWithSeconds(seconds, preferredTimescale: 60000)
fullReadPlayer?.currentItem?.seek(to: targetTime, toleranceBefore: .zero, toleranceAfter: .zero, completi[enter image description here][1]onHandler: { (finish) in
if finish {
completion?()
}
})
}
fullReadTimeObserver = fullReadPlayer?.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: 10), queue: DispatchQueue.main, using: { [weak self](time) in
guard let self = self else { return }
if self.fullReadPlayer?.status == .readyToPlay {
self.delegate?.audioPlayer(self, didUpdateCurrentTime: time.seconds, teach: nil)
}
})

当我搜索到4.57秒时,将首先显示正确的当前时间,然后当前时间将向前0.2秒,但播放将在当前时间向前0.2秒后开始。

日志:

当前时间:1.303104062

当前时间:1.401042787

寻求:4.579999923706055

当前时间:1.498295786

当前时间:4.579983333334

当前时间:4.319330793

当前时间:4.319642834

当前时间:4.401050459

当前时间:4.501045084

当前时间:4.601038959

我认为NSGangster有道理。观察者定时器和搜寻在不同的过程中。这对我来说意味着我将不得不自己处理这些差异。

我在互联网上没有找到答案,也没有找到纠正这个问题的方法,但我确实找到了一个变通办法:我可以使用"isSeekInProgress"这样的变量来标记是否更新进度条UI。图示如下:

var isSeekInProgress: Bool = false  // Marker  
let targetTime:CMTime = CMTimeMakeWithSeconds(seconds, preferredTimescale: 60000)
isSeekInProgress = true // Mark
fullReadPlayer?.currentItem?.seek(to: targetTime, toleranceBefore: .zero, toleranceAfter: .zero, completi[enter image description here][1]onHandler: { (finish) in
if finish {
isSeekInProgress = false // Unmark
completion?()
}
})

有些人在寻找时暂停播放器,然后在完成块中恢复播放,但如果你想在清理时继续播放,这是行不通的。上面的例子是相当无痛的,你所要做的就是检查:

if !playbackManager.isSeekInProgress {
progressSlider.value = playbackManager.progress
}

在下一次定期观察员通知期间,您的进度将得到更新。

最新更新