sc24evr
sc24evr

Reputation: 109

SKAction playsoundfilenamed fails to load sound

No matter what wav file I tried to play in an project, I keep getting the same error. The error states: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Resource namedfile.wav can not be loaded'

I cannot get any sound of any kind to load using SKAction.playSoundFilenamed. I have made sure that the file is names correctly and that doesn't seem to be the problem.

I have tested this in several projects, including the following test Game project wherein I use all default code except for a call to the SKAction

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        let myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "Hello, World!";
        myLabel.fontSize = 65;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
        let soundfile = SKAction.playSoundFileNamed("soundProject.wav", waitForCompletion: false)
        runAction(soundfile)
        self.addChild(myLabel)
    }

I cannot get any sound of any kind to load using SKAction.playSoundFilenamed. I already checked to made sure that the file is named correctly and that it exits in the bundle. Any help would be greatly appreciated. Thank you

UPDATE I Ran my attached code on a different computer, and it compliled and ran perfectly. There must be something wrong with my xcode/simulator. Does anyone know how to reset it? Thanks

Upvotes: 11

Views: 11166

Answers (6)

ingconti
ingconti

Reputation: 11646

for swift 3.0:

    SKAction.run {
      SKAction.playSoundFileNamed("soundProject.wav",
      waitForCompletion: false)
    }

and of course, verify if the resource is in project/copy Phases.

Upvotes: 2

Muindor
Muindor

Reputation: 347

For me, this was the issue. Both of these compiled fine and gave no errors, but only the latter actually played the sound.

Did not work:

SKAction.playSoundFileNamed("combo.mp3", waitForCompletion: true)

Did work:

node.run(SKAction.playSoundFileNamed("combo.mp3", waitForCompletion: true))

Good luck!

Upvotes: 3

user3313360
user3313360

Reputation: 93

What color is your Sounds folder?

If it's blue, it means it's a Folder Reference.
If it's yellow, it's a Group.

You Choose Folder Reference -vs- Group when you import your folder.

I've found that SKAction.playSoundFilenamed functions correctly only when it's loaded as a Group (Yellow).

Upvotes: 9

sc24evr
sc24evr

Reputation: 109

I identified the issue. I had to change the sound settings on my mac through the system preferences. Once I changes all of the settings to use only the internal speakers, the problem was resolved.

Upvotes: -1

lchamp
lchamp

Reputation: 6612

Here is another lead that might help you.

Sometimes SKAction is not very handy with audio files. I haven't found in which case exactly (might be : size file, sound length, ...).

In that case, you would want to use AVAudioPlayerinstead of it.

In order to not write your own "player", I suggest you to use an existing one. Here is one I've already used (SKTAudio) : https://github.com/raywenderlich/SKTUtils/blob/master/SKTUtils/SKTAudio.swift

Here is how to use it :

// For background audio (playing continuously)
SKTAudio.sharedInstance().playBackgroundMusic("music.wav") // Start the music
SKTAudio.sharedInstance().pauseBackgroundMusic() // Pause the music
SKTAudio.sharedInstance().resumeBackgroundMusic() // Resume the music

// For short sounds
SKTAudio.sharedInstance().playSoundEffect("sound.wav") // Play the sound once

Please let me know if the file is not played even with AVAudioPlayer. If so, it might be a problem with it and not the way it's played.

Upvotes: 2

lchamp
lchamp

Reputation: 6612

When you select your sound file in XCode, access the inspector on the right side, then make sure the file is selected for your target(s).

CaptureTarget

Upvotes: 34

Related Questions