Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioPlayer getting wrong file duration iOS 10

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)

enter image description here

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

like image 285
Gautam Sareriya Avatar asked Nov 08 '22 22:11

Gautam Sareriya


1 Answers

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

like image 138
samwize Avatar answered Nov 15 '22 06:11

samwize