Working on a project, I decided to add some music to play when a certain action is triggered. Here's what I've got:
var player : AVAudioPlayer?
several lines later...
func playSound(){
let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "Kalimba", ofType: "wav")!)
print(alertSound)
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try! AVAudioSession.sharedInstance().setActive(true)
try! player = AVAudioPlayer(contentsOf: alertSound)
player!.prepareToPlay()
player!.play()
}
Whenever I try this, xcode throws me a fatal error when the action is triggered, saying:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value