So, I'm using this function to set AVAudioPlayer:
func setupAudioPlayerWithFile(file: String) -> AVAudioPlayer? {
var audioPlayer: AVAudioPlayer?
if let sound = NSDataAsset(name: file) {
do {
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try! AVAudioSession.sharedInstance().setActive(true)
try audioPlayer = AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeWAVE)
} catch {
print("error initializing AVAudioPlayer")
}
}
return audioPlayer
}
But I'm getting hundreds of reported crashes from users. I'm not able to replicate any crash.
The crashes happen on these two lines:
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try! AVAudioSession.sharedInstance().setActive(true)
sometimes it crashes on the first line, sometimes on the second. How do I fix this? What could be causing these crashes?