@Steve, this is actually something it also happens to me, sometimes I feel a drop on the framerate when I play a sound for the first time, I am sure it's my code, I basically did a small sound engine using the AVAudioPlayer for background mp3 files and the AudioServices for simple wav effects, here is some code:
#import "Sound.h"
@implementation Sound
+(id) createWithContentsOfFile:(NSString *) path {
return [[[self alloc] initWithContentsOfFile: path] autorelease];
}
-(id) initWithContentsOfFile:(NSString *) path {
self = [super init];
if (self != nil) {
NSURL * filepath = [[NSURL alloc] initFileURLWithPath: path];
NSRange range = [path rangeOfString:@".mp3" options:NSCaseInsensitiveSearch];
if (range.location > 0) {
useSystemSound = NO;
} else {
useSystemSound = YES;
}
//useSystemSound = NO;
if (useSystemSound) {
AudioServicesCreateSystemSoundID((CFURLRef)filepath, &soundID);
} else {
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filepath error:NULL];
}
[filepath release];
}
return self;
}
-(BOOL) prepare {
if (useSystemSound) {
return NO;
} else {
return [audioPlayer prepareToPlay];
}
}
-(void) play {
if (!playing) {
if (useSystemSound) {
AudioServicesPlaySystemSound(soundID);
} else {
audioPlayer.numberOfLoops = 0;
if (!audioPlayer.playing) [audioPlayer play];
}
playing = YES;
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(onCompletion:) userInfo:nil repeats:NO];
}
}
-(void) onCompletion:(NSTimer *) sender {
playing = NO;
}
-(void) setVolume:(float)volume {
if (useSystemSound) {
} else {
audioPlayer.volume = volume;
}
}
-(void) loop {
if (useSystemSound) {
AudioServicesPlaySystemSound(soundID);
} else {
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
}
}
-(void) stop {
if (useSystemSound) {
} else {
[audioPlayer stop];
}
}
-(void) dealloc
{
if (useSystemSound) {
AudioServicesDisposeSystemSoundID(soundID);
} else {
[audioPlayer stop];
[audioPlayer release];
}
[super dealloc];
}
@end
I basically create an array of sounds encapsulated on a Mixer class, if it's a mp3 I use AVAudioPlayer, if not I use the AudioServices, and call the prepare (only affects the AVAudioPlayer really).
My understanding was that it's better to play uncompressed files for small effects, as they should be faster than mp3 files. For the background music I don't really care if there is a small delay anyway.
The first time I call one of the effects (AudioServices) I can feel the framerate drop.
Is not really bothering me too much, as it's only the very first time, actually, it's the very first time I call one effect (AudioServices), any, so after one of them is called, I can call any other effect with no delay, weird, like if I were missing some kind of preparation?