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.
Try calling player.prepareToPlay()
before you can get duration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With