gpt4 book ai didi

ios - 音频播放进度作为 Swift 中的 UISlider

转载 作者:IT王子 更新时间:2023-10-29 05:15:18 24 4
gpt4 key购买 nike

我看过一些关于在 Objective-C 中完成此操作的帖子,但我一直无法通过 Swift 完成同样的操作。

具体来说,我不知道如何在下面实现 addPeriodicTimeObserverForInterval

var player : AVAudioPlayer! = nil

@IBAction func playAudio(sender: AnyObject) {
playButton.selected = !(playButton.selected)
if playButton.selected {
let fileURL = NSURL(string: toPass)
player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
player.numberOfLoops = -1 // play indefinitely
player.prepareToPlay()
player.delegate = self
player.play()
startTime.text = "\(player.currentTime)"
endTime.text = NSString(format: "%.1f", player.duration)
} else {
player.stop()
}

如有任何帮助,我们将不胜感激。

最佳答案

感谢上面 Dare 的建议,我是这样完成的:

var updater : CADisplayLink! = nil

@IBAction func playAudio(sender: AnyObject) {
playButton.selected = !(playButton.selected)
if playButton.selected {
updater = CADisplayLink(target: self, selector: Selector("trackAudio"))
updater.frameInterval = 1
updater.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
let fileURL = NSURL(string: toPass)
player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
player.numberOfLoops = -1 // play indefinitely
player.prepareToPlay()
player.delegate = self
player.play()
startTime.text = "\(player.currentTime)"
theProgressBar.minimumValue = 0
theProgressBar.maximumValue = 100 // Percentage
} else {
player.stop()
}
}

func trackAudio() {
var normalizedTime = Float(player.currentTime * 100.0 / player.duration)
theProgressBar.value = normalizedTime
}

@IBAction func cancelClicked(sender: AnyObject) {
player.stop()
updater.invalidate()
dismissViewControllerAnimated(true, completion: nil)

}

关于ios - 音频播放进度作为 Swift 中的 UISlider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29542001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com