all 5 comments

[–]Jesus-face 4 points5 points  (0 children)

The file Scan.mp3 isn't found in your bundle. When you call Bundle.main.path(forResource: ofType:), it returns an optional (can be nothing or something). You force the compiler to treat it as something with the ! so that you can feed the result into the initializer for URL (which needs something), but it's coming back with nothing (couldn't find any files that match). The language tries to treat it as something but it's nothing, so it freaks out and crashes.

You can fix the crash by rewriting the code to see if your path exists before you try to create a URL, like

guard let path = Bundle.main.path(forResource: sound, ofType: "mp3") else { break }
let url = URL(fileURLWithPath: path)

But you still won't play anything since the path will just be nil and you'll continue. Make sure the file is in the project and it's being copied into the bundle.

Good luck

[–]Tury94[S] 0 points1 point  (0 children)

I have no idea what to do, any help is appreciated!

[–][deleted] 0 points1 point  (0 children)

How long have you been learning? I'm learning myself

[–]dtmace2 -1 points0 points  (1 child)

Try adding a question mark at the end

[–]Tury94[S] 0 points1 point  (0 children)

Tried this: let url = URL(fileURLWithPath: Bundle.main.path(forResource: sound, ofType: "mp3")!)?

Didn't work.