Reputation: 115
Was bug testing my app and noticed something interesting whenever a sound-file played (any sound file). I noticed that the smaller sound files would add about 0.01 mb of memory and not release the memory after, and the longer ones after done playing would add about 1.8 - 2.0 mb.
I used instruments leak check to test the app and see what exactly was going on and I gathered that it was indeed the audio-files.
I usually play my audio-files using an SKAction.playSoundFileNamed(). Not sure if that has anything to do with it.
let gameOver = SKAction.playSoundFileNamed("gameOver.wav", waitForCompletion: false)
sprite.runAction(gameOver)
Not sure if this is a problem with just me or if its a known problem. Or is it even a problem at all? Just was curious.
Upvotes: 1
Views: 509
Reputation: 165
I ran into this problem a few months ago, so I tried the AudioToolbox
framework as an alternative and it works just as well with no memory leaks.
I had a handful of sound effects I wanted to use for a game so I created a function like bellow which sets up all the systemSoundID
in the sfx array.
var sfx = [SystemSoundID]()
func createSfx() {
var i = 0
while i < 8 {
var fileName = ""
switch i {
case 0:
fileName = "coinCollected"
case 1:
fileName = "laserGun"
case 2:
fileName = "laserBeamShutOff"
case 3:
fileName = "powerDown"
case 4:
fileName = "shipMovement"
case 5:
fileName = "openBlockade"
case 6:
fileName = "invisibilityOn"
case 7:
fileName = "outOfFuel"
default:
print("Error creating sfx")
}
sfx.append(SystemSoundID())
let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "wav")
let soundURL = NSURL(fileURLWithPath: filePath!)
AudioServicesCreateSystemSoundID(soundURL, &sfx[i])
i += 1
}
}
You can then play a sound effect using AudioServicesPlaySystemSound
with the array index of the sound effect like:
AudioServicesPlaySystemSound(sfx[1]) // Plays laserGun sound effect
Also just so you know, there is one issue I found when using this. The volume of the sound effects won't increase past the halfway point on the device if your using an AVAudioPlayer
to play background music. I'm not sure why this happens but I wasted a lot of time trying to find an alternative for sound effects and this was the best bet, despite the slight volume issue.
Hope this helps some people that are looking for a good alternative to the SKAction.
Upvotes: 0