0

So in my previous question FOUND HERE, I was trying to find the most optimal way for playing a "jump" sound every time I make my sprite jump in my iOS Game...

For some reason today, I've become worried that I'm loading the audio file millions of time and wasting memory :(

Anywho, the following has been suggested to me, (so every time I jump by tapping the screen, the sound is loaded and played)...

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
    {
            //NSString *
            jumpSound = [[NSBundle mainBundle] pathForResource:@"boing" ofType:@"mp3"];

            //AVAudioPlayer *
            jumpAffect = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];

            [jumpAffect prepareToPlay];
            [jumpAffect play];

            //then, make sprite jump
            jumpUp = 16;
    }

The above method allows me to press the jump quickly and restart the audio loop even if the previous loop (from the previous jump) hasn't finished yet.

It's worth baring in mind that this could happen hundreds of times a minute (or more realistically a couple of times a second)...

Is this a concern? Do I need to release the tracks when done with the method/game loop?

Community
  • 1
  • 1
Reanimation
  • 3,151
  • 10
  • 50
  • 88

1 Answers1

0

I think your last question was totally incorrect. AVAudioPlayer has too much latency for a game. When you jump the sound might happen a second later instead of immediately when you want it to. If you wan't to play a sound a million times a minute you should use AudioServices as so:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     NSURL *pathURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"boing" ofType:@"wav"]];

     SystemSoundID audioEffect;
     AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
     AudioServicesPlaySystemSound(audioEffect);
     //then, make sprite jump
     jumpUp = 16;
}

You would have to convert your mp3 to a wav or caf. But I think the tradeoff is worth it...

keji
  • 5,947
  • 3
  • 31
  • 47
  • I can't seem to get this method to work when using the `-(void)touchesBegan...` – Reanimation Oct 10 '14 at 01:41
  • @Reanimation I adjusted my code to your situation. Make sure you have converted it to a wav file and have added it to your project – keji Oct 10 '14 at 01:45
  • @Reanimation also the mute button will have to be turned off for it to play. This is the one on the side of an iPhone – keji Oct 10 '14 at 01:46
  • Yeh, I've converted it and it's the exact code I have... I'm using the iOS simulator so can't tell if it's muted or not... I would of guess not by default so should be working... – Reanimation Oct 10 '14 at 01:49
  • @Reanimation do you think you could check on a device? There may be a problem with the Simulator. I would also check this question http://stackoverflow.com/questions/2720623/audioservicesplaysystemsound-not-playing-sounds – keji Oct 10 '14 at 01:54
  • A lot of people seem to have this people. Unfortunately I'm not able to try it on a device at the moment... – Reanimation Oct 10 '14 at 03:14