Reputation: 3417
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
Upvotes: 8
Views: 2513
Reputation: 2614
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)
}
}
Upvotes: -1
Reputation: 3417
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)
}
Upvotes: 10