views:

117

answers:

2

Hi, i'm coding in simple game where i move a square which performs a sound effect each time the square's moving. (like a walking sound)

Problem is playing the sound effect cause a display refresh lag. As if it was too much performance for the engine. I was asking if there's a way to properly play the repeatitive but not continuously sound (this is not really a loop, but it could be if the walking was endless)

here my function: ... the way i initialized the audio Object

moveSound = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

and here the function where it is called

...
 {
  moveSound.currentTime = 0;
  [moveSound play];
 } 

is there 's a good way to do it? thx

+1  A: 

In order to get lower latency audio, you should probably look into the CoreAudio API.

Your code would look something like this:

NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"NameOfSound" ofType:@"caf"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
SystemSoundID soundId;
AudioServicesCreateSystemSoundID((CFURLRef)audioURL, &soundId);

That soundId acts as a handle to the sound you created, so whenever you needed to play it you would call

AudioServicesPlaySystemSound(soundId);
Jamie Pinkham
A: 

Ok i found this:

Blockquote -(void)setSound {

CFBundleRef mainBundle = CFBundleGetMainBundle ();

CFURLRef soundFileURLRef = CFBundleCopyResourceURL (
                                            mainBundle,
                                            CFSTR ("squareMove"),// à mettre dans un xml ou plist
                                            CFSTR ("caf"),
                                            NULL
                                            );

AudioServicesCreateSystemSoundID (
                                  soundFileURLRef,
                                  &soundFileObject
                                  );
CFRelease(soundFileURLRef);

}

and i play the sound like

AudioServicesPlaySystemSound (soundFileObject);

Too bad we can't make midi

loïc