SpriteKit: Preload sound file into memory before playing?

前端 未结 1 649
轻奢々
轻奢々 2021-02-04 04:28

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\

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 05:09

    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.

    0 讨论(0)
提交回复
热议问题