Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build an Interactive Story App with Swift Adding Sound Effects Playing a System Sound

Ayo Omotosho
Ayo Omotosho
4,855 Points

App 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)

}

}

Ayo Omotosho
Ayo Omotosho
4,855 Points

Console: "fatal error: unexpectedly found nil while unwrapping an Optional value"

3 Answers

Good 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
Ayo Omotosho
4,855 Points

Thanks Marlon. App isn't crashing anymore. However, the sound effects aren't playing at all.

Thanks for your help though!

I 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.

I did the same thing, and Marlon is correct on both counts (thanks, Marlon!):

  1. Using a guard statement instead of force unwrapping is really the better way to go; and,
  2. 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)
    }
}

Glad 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
Emre Havan
8,569 Points

Thanks 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?

Sorry I'm now seeing this. Have you been able to fix the issue or do you still need help?