我正在用AVAudioPlayer播放一首歌。我需要一个进度条来显示歌曲的进度。
我的问题是进度条不能正常工作。在2-3秒内,它完成了它的进度。
func playMusic() {
do {
player = try AVAudioPlayer(contentsOf: (currentSong?.mediaURL)!)
guard let player = player else { return }
player.prepareToPlay()
player.play()
updater = CADisplayLink(target: self, selector: #selector(self.musicProgress))
updater.frameInterval = 1
updater.add(to: RunLoop.current, forMode: RunLoop.Mode.common)
playButton.setImage(UIImage.init(named: "pause"), for: .normal)
} catch let error as NSError {
print(error.description)
}
}
@objc func musicProgress() {
let normalizedTime = Float(self.player?.currentTime as! Double * 100.0 / (self.player?.duration as! Double) )
self.progressMusic.progress = normalizedTime
}发布于 2019-01-24 21:45:01
问题就在这里:
let normalizedTime = Float(self.player?.currentTime as! Double * 100.0 / (self.player?.duration as! Double) )这样,您将获得一个介于0.0和100.0之间的值,但是根据UIProgressView文档,progress必须介于0.0和1.0之间。试一试
let normalizedTime = Float(self.player?.currentTime as! Double / (self.player?.duration as! Double) )https://stackoverflow.com/questions/54347747
复制相似问题