ios - 如何跟踪 iOS 中的音轨进度?

标签 ios swift audio streaming podcast

我正在构建一个播客播放应用程序,它从已解析的 RSS 提要中获取其 mp3 URL。我正在使用 AVPlayer 毫无问题地播放文件,但我想弄清楚如何出于各种原因跟踪轨道的进度(更新进度 slider 的值,保存轨道的最后一个点当它停止时,点击播放不会重新启动音频...)

这就是我目前所了解的,通过我的研究,我只是不确定如何去做。如果有帮助,RSS 提要确实会给我 iTunes:Duration(例如 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()
    }
}

最佳答案

假设您已经正确地声明并约束了一个进度 View :`

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

然后使用 w 作为 View 的宽度。

关于ios - 如何跟踪 iOS 中的音轨进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34111238/

相关文章:

jQuery 切换类

ios - iOS siri快捷键Swift

ios - 如何在 ionic 中与其他应用程序共享文件

ios - 带有 iOS 圆形进度条的 AWS S3 下载

html - Swift:如何在 webView 中仅更改 <img> 或图像大小

c# - C#-.WAV随机播放高音

ios - KMLViewer Apple 的示例不起作用

swift - 动态检查对象是否支持动态成员查找

ios - 当 UITextView 放弃第一响应者时,导航栏项目不可点击

ajax - html5 音频 + javascript + ajax