AVAudioPlayer getting wrong file duration iOS 10

北城余情 提交于 2019-12-10 16:13:16

问题


I am using AVAudioPlayer object to load audio file. I have checked with two browser 1) chrome is getting exact duration. while 2) Safari is getting wrong file duration.

We have used the code as per below,

let soundData =  NSData(contentsOf: url as URL)
self.player = try AVAudioPlayer(data: soundData! as Data)                   
self.player.delegate = self
let currentSecond = self.player.duration

//Second Option we had tried.
 let assets = AVURLAsset(url: url as URL, options: [
 AVURLAssetPreferPreciseDurationAndTimingKey : Int(true)])
 var t = Float()
 t = Float(CMTimeGetSeconds(assets.duration))
 print(t)

Is there any suggestion? OR am I missing something? please guide me.


回答1:


Try calling player.prepareToPlay() before you can get duration.



来源:https://stackoverflow.com/questions/43797982/avaudioplayer-getting-wrong-file-duration-ios-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!