我有一个 AVPlayer 的时间观察者,效果很好。我试图让它从音频 0 的持续时间开始倒计时。
它可以工作并且确实倒计时现在我只是在格式化时遇到了一些麻烦。
如果你看到下面的图片,你就会明白我的意思。它从整分钟:秒 3:00 到奇怪的数字 3:-1 等。
任何想法我做错了什么?
这是定时器的代码:
let interval = CMTime(value: 1, timescale: 1)
self.audioPlayer?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { (progressTime) in
let secondsProgress = CMTimeGetSeconds(progressTime)
let secondsStringProgress = String(format: "%02d", Int((secondsProgress.truncatingRemainder(dividingBy: 60))))
let minuitesStringProgress = String(format: "%02d", Int(secondsProgress) / 60)
//self.countDownLabel.text = "\(minuitesStringProgress):\(secondsStringProgress)"
if let duration = self.audioPlayer?.currentItem!.asset.duration {
let secondsDuration = CMTimeGetSeconds(duration)
let secondsStringDuration = String(format: "%02d", Int((secondsDuration.truncatingRemainder(dividingBy: 60))))
let minuitesStringDuration = String(format: "%02d", Int(secondsDuration) / 60)
let secondsString = String(format: "%02d", Int(secondsStringDuration)! - Int(secondsStringProgress)!)
let minuitesString = String(format: "%02d", Int(minuitesStringDuration)! - Int(minuitesStringProgress)!)
self.countDownLabel.isHidden = false
self.countDownLabel.text = "\(minuitesString):\(secondsString)"
self.horizontalSlider.value = Float(secondsProgress / secondsDuration)
self.circleSlider.value = Float(secondsProgress / secondsDuration)
}
})