bshirley
bshirley

Reputation: 8357

SpriteKit runAction:withKey: combined with repeatActionForever: crashes app

The following code works (i can play a sound repeatedly indefinitely):

SKAction *playMusic = [SKAction playSoundFileNamed:@"sound.m4a" waitForCompletion:NO];
SKAction *onRepeat = [SKAction repeatActionForever:playMusic];
[myScene runAction:onRepeat];

The following code works (I can play a sound, keyed for later access):

SKAction *playMusic = [SKAction playSoundFileNamed:@"sound.m4a" waitForCompletion:NO];
[myScene runAction:onRepeat withKey:@"BackgroundMusic"];

The following code fails (I cannot key access to a repeated sound):

SKAction *playMusic = [SKAction playSoundFileNamed:@"sound.m4a" waitForCompletion:NO];
SKAction *onRepeat = [SKAction repeatActionForever:playMusic];
[myScene runAction:onRepeat withKey:@"BackgroundMusic"];

There is odd clicking instead of the sound being played, until the app crashes 10-15 seconds later ("terminated due to Memory error").

Does anyone know of this problem, or is this combination of use documented as not valid anywhere you know of?

Upvotes: 0

Views: 112

Answers (1)

bshirley
bshirley

Reputation: 8357

So, the fix is to change the waitForCompletion:YES.

SKAction *playMusic = [SKAction playSoundFileNamed:@"sound.m4a" waitForCompletion:YES];
SKAction *onRepeat = [SKAction repeatActionForever:playMusic];
[myScene runAction:onRepeat withKey:@"BackgroundMusic"];

Since the playSoundFileNamed length of action is 0, it continues to try to play it over and over, leading to unhappy sound and memory problems.

I'm not sure if the one repeat action is the only one registered with the key, or if each one being played is stomping on the previous one. Only really important for academic reasons.

Upvotes: 1

Related Questions