ziopiero
ziopiero

Reputation: 11

How play music in SceneKit

I tried to play music in my SceneKit game, but it crashes without a reason. So I try to add this 3 lines of code in the standard Apple game template (at the end of viewDidLoad):

let music = SCNAudioSource(fileNamed: "Music.mp3")
let action = SCNAction.playAudio(music!, waitForCompletion: false)
ship.runAction(action)

and, at runtime, Xcode show me this message: com.apple.scenekit.scnview-renderer (8): breakpoint 1.2

Where is my mistake?

I tried to compile and run my code in two different Mac: on my MacBookPro Retina it runs ok with the sound, in my iMac 21,5 it crashes. So, my code is correct, probably I will fill a radar to Apple. Note that both Macs are installed with OS Mojave in beta (same version) and also Xcode used is the Beta.

Upvotes: 1

Views: 675

Answers (2)

2147483647
2147483647

Reputation: 31

You have to put the audio file into an SCNAssets catalog (with .scnassets extension), like the art.scnassets catalog in the template. Then play the music like that:

if let source = SCNAudioSource(fileNamed: "art.scnassets/Music.mp3") {
    let action = SCNAction.playAudio(source, waitForCompletion: false)
    ship.runAction(action)
} else {
    print("cannot find file")
}

Upvotes: 3

TheCoderateKid
TheCoderateKid

Reputation: 92

I think you are looking for something like this...

ship.runAction(SKAction.playSoundFileNamed("Music.mp3",waitForCompletion:false));

Upvotes: -1

Related Questions