我正在快速构建我的第一个 IOS 应用程序,但我陷入了这个问题:流式传输时如何获取音乐文件的长度(持续时间)?
我做了很多研究,也写了一些代码来解决这个问题,但似乎我的代码不够好。
func prepareAudio() {
audioLength = CMTimeGetSeconds(self.player.currentItem.asset.duration)
playerProgressSlider.maximumValue = CFloat(CMTimeGetSeconds(player.currentItem.duration))
playerProgressSlider.minimumValue = 0.0
playerProgressSlider.value = 0.0
showTotalSurahLength()
} // i prepare for get the duration and apply to UISlider here
func showTotalSurahLength(){
calculateSurahLength()
totalLengthOfAudioLabel.text = totalLengthOfAudio
} // get the right total length of audio file
func calculateSurahLength(){
var hour_ = abs(Int(audioLength/3600))
var minute_ = abs(Int((audioLength/60) % 60))
var second_ = abs(Int(audioLength % 60))
var hour = hour_ > 9 ? "\(hour_)" : "0\(hour_)"
var minute = minute_ > 9 ? "\(minute_)" : "0\(minute_)"
var second = second_ > 9 ? "\(second_)" : "0\(second_)"
totalLengthOfAudio = "\(hour):\(minute):\(second)"
} // I calculate the time and cover it
任何曾经遇到过这个问题的人,你能给我一些解决问题的建议吗?我是 Swift 的新手,仍在学习以提高我的技能。
谢谢,
最佳答案
对于 swift :
let asset = AVURLAsset(URL: NSURL(fileURLWithPath: pathString), options: nil)
let audioDuration = asset.duration
let audioDurationSeconds = CMTimeGetSeconds(audioDuration)
关于ios - Swift: AVPlayer - 如何从 URL 获取 mp3 文件的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31155003/