8

I have a problem with sprite kit SKAction.playSoundFileNamed. In practice, after some time it is played correctly , the app crashes saying it will not load . The file is included in the bundle import the project file exists and everything is properly set.

The only problem , after some time I play, I will crash saying it can not find the file , or at least can not be loaded .

My question is , is there a way to recharge every time the sound SKAction.playSoundFileNamed ?

EDIT - SOLVED

//init
var sound = SKAction.playSoundFileNamed("sound.mp3", waitForCompletion: false)
var sound2 = SKAction.playSoundFileNamed("sound2.mp3", waitForCompletion: false) 

//in the code call function when play sound:
playSound(sound)

...

func playSound(soundVariable : SKAction)
{
    runAction(soundVariable)   
}

The preload sounds instantiated no longer generates crash

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110
  • This has been asked a few times before, i believe without resolution. My assumption is that this may be a bug. Also the playsound action is a crutch, a necessity to have some easy way to play sound. If you want any reasonable audio playback, use AVAudioPlayer or better a sound engine like ObjectAL. – CodeSmile Nov 01 '14 at 10:10
  • AVAudioPlayer with the sound stops when the next part, but I need to be reproduced in whole sounds even if they overlap. Solutions? – Mr. Developer Nov 02 '14 at 12:50

2 Answers2

10

prelound sound variable

//init
var sound = SKAction.playSoundFileNamed("sound.mp3", waitForCompletion: false)
var sound2 = SKAction.playSoundFileNamed("sound2.mp3", waitForCompletion: false) 

//in the code call function when play sound:
playSound(sound)

...

func playSound(soundVariable : SKAction)
{
    runAction(soundVariable)   
}
Mr. Developer
  • 3,295
  • 7
  • 43
  • 110
  • i had the same problem , then try to force the network so I solved . I assure you that it works and does not crash . Thanks to the next – Mr. Developer Jan 15 '15 at 15:36
  • Although this crashes less frequently, you will still get the occasional crash. This is an issue with apple and this solution greatly reduces the frequency of crashing and limits it to app launches (assuming that's when you preload the action). – Daniel K Aug 24 '15 at 19:38
  • I do not have reduced the frequency of bugs . I took it off . With this solution ( and use in other apps ) I've had no problems . Although I'm sure that with swift 2 and more open source , there will be many improvements on this aspect also – Mr. Developer Sep 02 '15 at 09:41
  • my god man you're my effing hero! you saved my game hahahaha :) thanks the sound was bugging like hell!!!! you deserve more upvotes. – msqar Nov 02 '16 at 23:51
-1

I have this little helper class of type SKNode for playing Audio files. NOTE: Helper object must be added to SKScene hierarchy for audios to be played.

import UIKit

enum SFX_TYPE:Int
{
    case NEW_LEVEL = 0
    case BANG = 1
}

let SFXContainer:[SFX_TYPE:[SKAction]] = [
    SFX_TYPE.NEW_LEVEL : [SKAction.playSoundFileNamed("newlevel.m4a", waitForCompletion: true)],
    SFX_TYPE.BANG : [
        SKAction.playSoundFileNamed("explosion1.m4a", waitForCompletion: true),
        SKAction.playSoundFileNamed("explosion2.m4a", waitForCompletion: true),
        SKAction.playSoundFileNamed("explosion3.m4a", waitForCompletion: true),
        SKAction.playSoundFileNamed("explosion4.m4a", waitForCompletion: true)
    ]
]

class SabilandSound: SKNode {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    deinit
    {
        Helper.masterObserverRemove(self)
    }

    override init()
    {
        super.init()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("masterPlaySFX:"), name: NCNPlaySFX, object: nil)
    }

    func masterPlaySFX(n:NSNotification)
    {
        let st = SFX_TYPE(rawValue: n.userInfo![NCNPlaySFX] as! Int)!
        let c = SFXContainer[st]!
        let a = SFXContainer[st]![Helper.randomBetween(0, max: c.count, includeMax: false)]
        runAction(a)
    }

}
sabiland
  • 2,526
  • 1
  • 25
  • 24