AVAudioPlayer not playing any sound
byThe most common culprit causing AVAudioPlayer not playing sound is ARC. When my audio player's scope is just within a function ARC deallocates the memory before playing sound after function is called. Here is an example code:
func playSoundEffect(assetName: string?) {
if let sound = assetName{
if let asset = NSDataAsset(name:sound){
do {
// Use NSDataAsset's data property to access the audio file stored in Sound.
let soundEffect = try AVAudioPlayer(data:asset.data, fileTypeHint:"wav")
// Play the above sound file.
soundEffect?.play()
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
}