SpriteKit:播放前将声音文件预加载到内存中?

ald*_*uin 16 objective-c ios ios7 sprite-kit

只是想知道这是否可行.目前,我第一次在应用程序运行时播放声音文件时,在声音实际播放之前会有明显的延迟(比如它正在缓存它或其他东西).在此之后它立即播放没有问题,但如果我完全关闭应用程序并重新启动它,延迟将在第一次播放声音时返回.这是我用来播放声音的代码:

[self runAction:[SKAction playSoundFileNamed:@"mySound.caf" waitForCompletion:NO]];
Run Code Online (Sandbox Code Playgroud)

Mas*_*uin 22

您可以采取的一种方法是在场景的开头加载声音:

YourScene.h:

@interface YourScene : SKScene
@property (strong, nonatomic) SKAction *yourSoundAction;
@end
Run Code Online (Sandbox Code Playgroud)

YourScene.m:

- (void)didMoveToView: (SKView *) yourView
{
    _yourSoundAction = [SKAction playSoundFileNamed:@"yourSoundFile" waitForCompletion:NO];
    // the rest of your init code
    // possibly wrap this in a check to make sure the scene's only initiated once...
}
Run Code Online (Sandbox Code Playgroud)

这应该预加载声音,你应该能够通过调用场景中的动作来运行它:

[self runAction:_yourSoundAction];
Run Code Online (Sandbox Code Playgroud)

我在一个有限的场景中尝试了这个,它似乎摆脱了延迟.