import Foundation
import AVFoundation
class SoundManager {
var audioPlayer:AVAudioPlayer?
enum SoundEffect {
case flip
case match
case nomatch
case shuffle
}
func playSound(effect:SoundEffect) {
var soundFileName = ""
switch effect {
case .flip:
soundFileName = "cardflip"
case .match: soundFileName = "dingcorrect"
case .nomatch: soundFileName = "dingwrong"
case .shuffle: soundFileName = "shuffle"
}
// Get the path to the resource
let bundlePath = Bundle.main.path(forResource: soundFileName, ofType: ".wav")
// Check that its not nil
guard bundlePath != nil else {
return
}
let url = URL(fileURLWithPath: bundlePath!)
do {
// Create the Audio Player
audioPlayer = try AVAudioPlayer(contentsOf: url)
// Play the Sound Effect
audioPlayer?.play()
}
catch {
print("Couldn't create an Audio Player")
return
}
}
}
It could be this line that is the culprit:
let bundlePath = Bundle.main.path(forResource: soundFileName, ofType: ".wav")
Remove the . before wav
so it looks like this:
let bundlePath = Bundle.main.path(forResource: soundFileName, ofType: "wav")
No, still does the error. probably not critical as the app works, but like to know why it does it.
Yeah so would I.
What version of Xcode are you using?
Version 11.6 (11E708) …
Yeah same here though I just updated to 11.7 because I got a notification from AppStore that there was an update. Same same.
Have you declared an instance of the SoundManager in your ViewController like this? I’m covering all bases to try and figure out what might be the issue.
var soundPlayer = SoundManager()
Yep, certainly have that in view controller
I’ve just about run out of ideas.
Try cleaning your project build folder by using the keyboard command sequence
Shift + Option + Command + K
It’s just a stab in the dark.
Nope lol, still have the error. I have to go out, I’ll be an hour or so. Not sure what time you are but all good if you don’t reply till tomorrow.
No worries. It’s Midday in Perth, Western Australia so chat you later.
I’m all out of ideas right now anyway.