xcode - found nil while unwrapping an Optional value when trying to play a sound -


i trying play sound in os x app has been built in swift.

i getting following error:

fatal error: unexpectedly found nil while unwrapping optional value 

the line of code in question is:

let bgmusicurl:nsurl = nsbundle.mainbundle().urlforresource("emptytrash", withextension: "aif")! 

i have sound located here here

however when call method prepare , play sound:

 func playsound() {         let bgmusicurl:nsurl = nsbundle.mainbundle().urlforresource("emptytrash", withextension: "aif")!         backgroundmusicplayer = try!  avaudioplayer(contentsofurl: bgmusicurl)         backgroundmusicplayer.numberofloops = -1         backgroundmusicplayer.preparetoplay()             backgroundmusicplayer.play()     } 

i error. going on?

i declare this:

var backgroundmusicplayer:avaudioplayer = avaudioplayer() 

a file part of project doesn't have part of target, means won't copied application bundle.

to see "target membership", can open sidebar on right side (file inspector) of window. see viewing properties of file more information.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -