11

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

sc24evr
  • 109
  • 1
  • 1
  • 5

7 Answers7

34

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

lchamp
  • 6,592
  • 2
  • 19
  • 27
  • I check the target as suggestioned and the correct project target was selected. I think Xcode knows that the file exists but is failing to load it. For example, if I intentionaly misspell the file name in he code, I get an error saying the file isn't found. Here rather I get an error saying the resource "can not be loaded". – sc24evr Mar 03 '15 at 18:15
  • Is the sound played only once or somewhere else ? Just to be sure it's crashing here. I'm trying to look is there is any restriction about the : sound time length, sound file size, sound bitrate, ... Can you give me those informations for you file ? Or even upload it so I can try to play it in my own app. – lchamp Mar 03 '15 at 18:23
  • I've suggested an other lead in a second response. Please give a look at it and let me know if it helped. If it didn't, please give me more information about the file or upload it. – lchamp Mar 03 '15 at 18:41
  • This solves my problem, thanks. When I drag `m4a` into project, the target is unmarked by default, but for `mp3` or `CAF` file the target is marked by default. Is it the normal behavior of Xcode? – Zhou Haibo Nov 14 '20 at 03:53
9

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).

user3313360
  • 93
  • 1
  • 3
  • yes, for "blue" folder, You will have to build all path using: `let myBundlePath = Bundle.main.bundlePath let pathInBundle = myBundlePath+"/"+mediaFolder+"/"+name` – ingconti Aug 19 '16 at 14:31
  • I found the same issue, blue folder will not work with `SKAction.playSound...`. We should use yellow one (Group) when dragging some resources folder into the project, though it is weird. – Zhou Haibo Dec 18 '20 at 15:25
3

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!

Muindor
  • 177
  • 1
  • 9
2

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.

lchamp
  • 6,592
  • 2
  • 19
  • 27
  • I tried that and still couldn't get the sound to play. I uploaded the project i made to test this at https://github.com/sc24evr/TestingSound1 . I still received an error sadly "TestingSound1[28834:778122] 18:40:39.824 ERROR: 168: EXCEPTION thrown ('fmt?'):" – sc24evr Mar 03 '15 at 23:43
  • Here is the project that I try to use the SKAction where I keep getting errors. https://github.com/sc24evr/TestAudioSpriteKit – sc24evr Mar 03 '15 at 23:51
2

for swift 3.0:

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

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

Lin Du
  • 88,126
  • 95
  • 281
  • 483
ingconti
  • 10,876
  • 3
  • 61
  • 48
-1

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.

sc24evr
  • 109
  • 1
  • 1
  • 5
  • Ya definitly a weird issue. Thanks again for all your suggestions. They helped me narrow down the problem significantly. – sc24evr Mar 08 '15 at 22:40
-1

swift 5:

Instead of

SKAction.repeatForever(SKAction.playSoundFileNamed("Sounds/theme.mp3",
                                                   waitForCompletion: true))

I wrote

SKNode().run(
    SKAction.repeatForever(SKAction.playSoundFileNamed("Sounds/theme.mp3",
                                                       waitForCompletion: true))
)

And it works good.

itMaxence
  • 1,230
  • 16
  • 28
Tatsiana
  • 64
  • 1
  • 4