Tiz
Tiz

Reputation: 41

SpriteKit doesn't play any music with either SKAudioNode or SKAction.playSoundFileNamed

I'm struggling with a problem, that neither SKAudioNode nor SKAction.playSoundFileNamed could help me with playing a music, in a project. The audio files format are mp3, wav or m4a

I checked if it works another project. But still it occur. I think it would be a reproducible problem so I'll attach my codes here (* if this, the codes below, worked on your pc please notice me in a comment. )


import SpriteKit
import GameplayKit
import AVFoundation  // Needed...?

class GameScene: SKScene {

    override func didMove(to view: SKView) {
        let foo = SKAudioNode(fileNamed: "sample")
        self.addChild(foo)
    }

    override func keyDown(with event: NSEvent) {
        switch event.keyCode {
        case 49:
            let bar = SKAction.playSoundFileNamed("bubble", waitForCompletion: false)
            self.run(bar)
        default:
            print("keyDown: \(event.characters!) keyCode: \(event.keyCode)")
        }
    }


    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }
}

I just would like to play sample.wav as background music and when Space is pressed, play bubble.mp3 as a sound effect

Error outputs below

[AudioHAL_Client] AudioHardware.cpp:666:AudioObjectGetPropertyData:  AudioObjectGetPropertyData: no object with given ID 0
SKAction: Error loading sound resource: "bubble"

Thank you.

Upvotes: 0

Views: 430

Answers (1)

Tiz
Tiz

Reputation: 41

In my case Audio files are in Assets.xcassets. but that's wrong. Just put all audio files under project group.

Upvotes: 3

Related Questions