如何使用SKAudioNode

时间:2016-03-22 17:25:06

标签: ios objective-c sprite-kit skaudionode

我发现SKAction playSoundFileNamed会在IOS 9中发生内存泄漏: https://forums.developer.apple.com/thread/20014

他们建议使用SKAudioNode,但示例是swift,我在项目中使用objective-c。

示例:

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

我尝试了什么:

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

SKAction *playaction = [SKAction play];

[testSound runAction:playaction];
}

它会崩溃到:

[self addChild:testSound];

那么我将如何使它工作,只有在IOS 9中使用SKAudioNode播放声音的好技巧>和旧版本的SKAction?

谢谢!

1 个答案:

答案 0 :(得分:2)

方法+ 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