19

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]];
alduin
  • 317
  • 3
  • 11
  • I would suggest that you use Instruments to see where the delay might be happening. Use the "Time Profiler" Instrument to see what code is taking the most amount of time and also use the CPU-Strategy-View to see what is happening on the CPU between the time of interest. From there, you may post, if you choose, the problem spots found by profiling the app. – AhiyaHiya Apr 03 '14 at 02:30

1 Answers1

24

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.

MassivePenguin
  • 3,701
  • 4
  • 24
  • 46
  • 1
    Thanks so much for this answer! This same issue was killing me. – Scooter Aug 10 '14 at 15:09
  • In my experience, you don't need to "hold onto" a reference to the action. It seems that the SpriteKit is holding onto the loaded audio once any instance of play sound for that file is created. – bshirley Dec 08 '14 at 20:45
  • No exactly. I just executed [SKAction playSoundFileNamed:@"yourSoundFile" waitForCompletion:NO]; in my application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions – Fredrik Johansson May 07 '15 at 15:07