6

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!

4

1 回答 1

3

方法+ nodeWithFileNamed:通过从游戏的主包中加载存档文件来创建一个新节点。所以你不能在这种情况下使用它。

尝试这样的事情(使用initWithFileNamed初始化程序):

#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
于 2016-03-22T18:03:14.093 回答