我正在使用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
}

最佳答案

问题在这里:

let normalizedTime = Float(self.player?.currentTime as! Double * 100.0 / (self.player?.duration as! Double) )

这样,您将获得一个介于0.0100.0之间的值,但是根据UIProgressView文档,progress必须介于0.01.0之间。尝试
let normalizedTime = Float(self.player?.currentTime as! Double / (self.player?.duration as! Double) )

关于ios - 如何使用进度栏显示带有进度条的AVAudioPlayer?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54347747/

10-16 14:10