Play Sound in Swift SpriteKit project?

2019-02-19 03:35发布

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'

2条回答
疯言疯语
2楼-- · 2019-02-19 03:38

I had this exact problem for the last hour! I had to go a different route, because I tried multiple things to get this simple line of code to work and failed. It's a little more code, but this solved my problem: https://stackoverflow.com/a/24073071/586204.

查看更多
叼着烟拽天下
3楼-- · 2019-02-19 04:01

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

查看更多
登录 后发表回答