I am trying to play sound with AvPlayer. I am using following method:
func playSound() {
let coinSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("voiceRecord", ofType: "caf")!)
do{
let audioPlayer = try AVAudioPlayer(contentsOfURL:coinSound)
audioPlayer.prepareToPlay()
audioPlayer.play()
}catch {
print("Error getting the audio file")
}
}
I am sure I have voiceRecord.caf file in my app's Documents folder. I am getting following error for first line.
fatal error: unexpectedly found nil while unwrapping an Optional value
How to resolve?