SpriteKit: Preload sound file into memory before playing?

你说的曾经没有我的故事 提交于 2019-12-20 12:29:16

问题


Just wondering if this is possible. Currently, the first time I play a sound file while the app is running, there is a noticeable delay before the sound actually plays (like it's caching it or something). After this it plays instantly without issue, but if I close the app completely and relaunch it, the delay will be back the first time the sound is played. Here is the code I'm using to play the sound:

[self runAction:[SKAction playSoundFileNamed:@"mySound.caf" waitForCompletion:NO]];

回答1:


One approach you could take is to load the sound in right at the beginning of the scene:

YourScene.h:

@interface YourScene : SKScene
@property (strong, nonatomic) SKAction *yourSoundAction;
@end

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...
}

This should preload the sound, and you should be able to run it by calling the action on your scene:

[self runAction:_yourSoundAction];

I've tried this myself in a limited scenario and it appears to get rid of the delay.



来源:https://stackoverflow.com/questions/22826675/spritekit-preload-sound-file-into-memory-before-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!