AVAudioPlayer gets wrong iOS 10 file duration

I am using AVAudioPlayer object to load an audio file. I checked with 2 browsers 1) chrome gets the exact duration. while 2) Safari is getting the wrong file length.

We have used the code as 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)

      

enter image description here

Any suggestion? OR am I missing something? please guide me.

+4


source to share


1 answer


Try to summon player.prepareToPlay()

before you can get the duration.



0


source







All Articles