我正在使用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_ > 9 ? "\(minute_)" : "0\(minute_)"
var second = second_ > 9 ? "\(second_)" : "0\(second_)"
totalLengthOfAudio = "\(hour):\(minute):\(second)"
} // I calculate the time and cover it
有没有遇到过这个问题的人,可以给我一些修复建议吗?我在Swift方面很新,还在学习提高我的技能。
谢谢!