Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Sound in Swift SpriteKit project?

When I run this code.

runAction(SKAction.playSoundFileNamed("Click.mp3", waitForCompletion: false))

My app was Crashed:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Resource Click.mp3 cannot be found in the main bundle'

like image 537
VIẾT NAM Avatar asked Nov 06 '14 12:11

VIẾT NAM


1 Answers

verify that the file exists in the project. instantiates the variables in the file first and then make a method for breeding.

if the file exists in the project you should do the following:

var sound = SKAction.playSoundFileNamed("sound.mp3", waitForCompletion: false)
...
override func didMoveToView(view: SKView) {
    playSound(sound)
    //call playSound method when you want
}

func playSound(sound : SKAction)
{
    runAction(sound)
}

in this mode don't crash

like image 169
Mr. Developer Avatar answered Oct 06 '22 08:10

Mr. Developer