简体   繁体   English

如何跟踪iOS中的音轨进展?

[英]How to keep track of audio track progression in iOS?

I'm building a podcast playing app that fetches its mp3 URL from a parsed RSS feed. 我正在构建播客播放应用程序,从解析的RSS源中获取其mp3 URL。 I'm using AVPlayer to play the file without issue, but I'd like to figure out how to keep track of the progression of the track for various reasons (updating the value of the progress slider, saving the last spot the track was on when it was stopped so that hitting play doesn't restart the audio...) 我正在使用AVPlayer播放文件而没有问题,但我想弄清楚如何通过各种原因跟踪轨道的进展(更新进度滑块的值,保存轨道所在的最后一个位置)当它被停止,以便击中播放不重启音频......)

This is what I've got so far, and through my research I'm just not sure how to go about doing this. 这是我到目前为止所做的,通过我的研究,我不知道如何去做。 The RSS feed does give me iTunes:Duration (01:24:20 for example) if that helps. 如果有帮助,RSS源确实给我iTunes:持续时间(例如01:24:20)。

    @IBAction func playPauseButtonPressed(sender: UIButton) {

    if playPauseButton.titleLabel!.text == "Play" {
        playPauseButton.setTitle("Pause", forState: .Normal)
        let playerItem = AVPlayerItem(URL: trackURL!)
        audioPlayer = AVPlayer(playerItem: playerItem)
        audioPlayer.rate = 1.0
        audioPlayer.play()
    } else {
        playPauseButton.setTitle("Play", forState: .Normal)
        audioPlayer.pause()
    }
}

Assuming you've declared and constrained a progress view properly:` 假设您已正确声明并约束了进度视图:`

let total = audioPlayer.duration
let f = audioPlayer.currentTime / total;
let w = view.frame.width * CGFloat(f)

Then use w as the width of your view. 然后使用w作为视图的宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM