Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ayo Omotosho
4,855 PointsApp Crashing after adding SoundEffects Code
After adding all the code for the sound effects as instructed by Pasan, once the app loads up and i input a name and click continue, the App just crashes. I think it has something to do with a force unwrapped line of code in there, not really sure though as i am still a novice at this. Some assistance with this would be highly appreciated.
Audio.SwiftFile
import Foundation import AudioToolbox
extension Story {
var soundEffectName: String {
switch self {
case .droid, .home: return "Happy Ending"
case .monster: return "Ominous"
default: return "Page Turn"
}
}
var soundEffectURL: URL {
let path = Bundle.main.path(forResource: soundEffectName, ofType: "wav")!
return URL(fileURLWithPath: path)
}
}
class SoundEffectsPlayer { var sound: SystemSoundID = 0
func playSound(for story: Story) {
let soundURL = story.soundEffectURL as CFURL
AudioServicesCreateSystemSoundID(soundURL, &sound)
AudioServicesPlaySystemSound(sound)
}
}
3 Answers

Marlon Henry
6,885 PointsGood old guard statement can help
var soundEffectURL: URL {
guard let path = Bundle.main.path(forResource: soundEffectName, ofType: "wav") else{ return URL(string:"in here put a value you know works")!}
return URL(fileURLWithPath: path)
}

Ayo Omotosho
4,855 PointsThanks Marlon. App isn't crashing anymore. However, the sound effects aren't playing at all.
Thanks for your help though!

Marlon Henry
6,885 PointsI would say look at the spelling and spacing in your string names. If those are not exactly as you have those wav files in your project then it won't work, I also advise against having spaces in your file names if you can avoid it.

kols
26,899 PointsI did the same thing, and Marlon is correct on both counts (thanks, Marlon!):
- Using a guard statement instead of force unwrapping is really the better way to go; and,
- The crash, in this case, is caused by having spaces between words (e.g., "Happy Ending" instead of "HappyEnding") in that initial Story extension...
Pasting correct code below:
extension Story {
var soundEffectName: String {
switch self {
case .droid, .home: return "HappyEnding"
case .monster: return "Ominous"
default: return "PageTurn"
}
}
// Note: A guard statement would be ideal instead of force unwrapping as shown below:
var soundEffectURL: URL {
let path = Bundle.main.path(forResource: soundEffectName, ofType: "wav")!
return URL(fileURLWithPath: path)
}
}

Ayo Omotosho
4,855 PointsThanks.

Marlon Henry
6,885 PointsGlad I could help. I code all day at work, so I just cruise the forums to help folks since I didn't have much help when I started in my early days.

Emre Havan
8,569 PointsThanks a lot @Marlon! I could get rid of the error, but my code still does not play any sound when I go through the story. Anyone has an idea why this might be happening?

Marlon Henry
6,885 PointsSorry I'm now seeing this. Have you been able to fix the issue or do you still need help?
Ayo Omotosho
4,855 PointsAyo Omotosho
4,855 PointsConsole: "fatal error: unexpectedly found nil while unwrapping an Optional value"