I’m working with sounds for the first time, and for some reason, Swift can’t find my .wav file in my project. The file name is Test_Bell.wav”, and here’s my code, which is failing as it tries to force-unwrap the path variable:
class SoundManager {
var audioPlayer = AVAudioPlayer()
let hintSolvedFileName = “Bell_Test.wav"
@Published private(set) var soundEffectsEnabled = false
@Published private(set) var musicEnabled = false
func playHintSolved() {
//force unwrapped path
let path = Bundle.main.path(forResource: hintSolvedFileName, ofType: nil)!
let url = URL(fileURLWithPath: path)
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer.play()
} catch {
print("SoundManager.playHintSolved couldn't play")
fatalError("Coudln't play file")
}//catch
}//func
}//class
I’ve confirmed that my file name is correct, and even confirmed that I can play the sound, by selecting it in the Navigator, viewing it in the Editor, and playing the sound.
Is there a specific area I need to place my file In my project? Right now, it’s just inside my project, where all of my Swift and JSON files are. I’m stumped as to why it can’t find the path my my file.