ios – 如何使用SKAudioNode

我发现SKAction playSoundFileNamed会在
IOS 9中发生内存泄漏:

https://forums.developer.apple.com/thread/20014

他们建议使用SKAudioNode,但是示例很快,我在项目中使用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?

谢谢!

最佳答案 方法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
点赞