我正在使用swift构建我的第一个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_ > …Run Code Online (Sandbox Code Playgroud)