7

I found out SKAction playSoundFileNamed does memory leak in IOS 9: https://forums.developer.apple.com/thread/20014

They recommend to use SKAudioNode, but the example is swift and i use objective-c in my project.

Example:

func testAudioNode() {  
    let audioNode = SKAudioNode(fileNamed: "LevelUp")  
    audioNode.autoplayLooped = false  
    self.addChild(audioNode)  
    let playAction = SKAction.play()  
    audioNode.runAction(playAction)  
}  

What i have tried:

-(void)testSound{
testSound = [SKAudioNode nodeWithFileNamed:@"test.wav"];
testSound.autoplayLooped = false;
[self addChild:testSound];

SKAction *playaction = [SKAction play];

[testSound runAction:playaction];
}

It will crash to:

[self addChild:testSound];

So how i would get it work, what is good technique for play sounds with SKAudioNode only in IOS 9> and with SKAction in older versions?

Thanks!

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
TeKo
  • 97
  • 1
  • 3

1 Answers1

3

Method + nodeWithFileNamed: creates a new node by loading an archive file from the game’s main bundle. So you can't use it in this situation.

Try something like this (using initWithFileNamed initializer):

#import "GameScene.h"

@interface GameScene()
@property (nonatomic, strong)SKAudioNode *testSound;
@end

@implementation GameScene

-(void)didMoveToView:(SKView *)view{

     self.testSound = [[SKAudioNode alloc] initWithFileNamed:@"test.wav"];
     self.testSound.autoplayLooped = false;
     [self addChild:self.testSound];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

   [self.testSound runAction:[SKAction play]];

}
@end
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • Thanks for quick answer! Some reason your code is not working in my actual project, but i tested it in new(empty)project and it worked fine! It crash immediately when game scene loads...any doubts ? – TeKo Mar 22 '16 at 21:14
  • @TeKo It works for me as well... You have to be more specific about the actual error. – Whirlwind Mar 22 '16 at 21:17
  • it says: Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine->IsRunning()' – TeKo Mar 22 '16 at 21:37
  • I got it fixed! Problem was playing a sound instantly after scene transition. https://forums.developer.apple.com/thread/27980#jive-2401624518966197692552 Thank you very much for the help! – TeKo Mar 22 '16 at 22:01