When I try to play a .wav
file from server using AVQueuePlayer it returns duration as nan
.
I used this code to get the duration
audioPlayer.currentItem?.duration.seconds
audioPlayer.currentItem?.asset.duration.seconds
And I am sure that .wav
file I am using is not corrupted since its being used by android app and they are able to get the duration.
Below code that I used works perfectly for .mp3
. but not for .wav
if let url = URL(string: urlString) {
audioAsset = AVURLAsset.init(url: url)
if let audioAsset = audioAsset {
let keys = ["playable"]
audioAsset.loadValuesAsynchronously(forKeys: keys, completionHandler: {
let playableStatus = audioAsset.statusOfValue(forKey: keys[0], error: nil)
switch playableStatus {
case .unknown, .loading, .failed, .cancelled:
return
case .loaded:
DispatchQueue.main.async {
let playerItem = AVPlayerItem.init(asset: audioAsset)
self.audioPlayer.insert(playerItem, after: nil)
self.audioPlayer.play()
})
}
break
}
})
}
}