I\'m trying to eliminate startup lag when playing a (very short -- less than 2 seconds) audio file via AVAudioPlayer on the iPhone.
First, the code:
I've taken an alternative approach that works for me. I've seen this technique mentioned elsewhere (tho I don't recall at the moment where that was).
In short, preflight the sound system by loading and playing a short, "blank" sound before you do anything else. The code looks like this for a short mp3 file I preload in my view controller'sviewDidLoad
method that's of .1 second duration:
NSError* error = nil;
NSString* soundfilePath = [[NSBundle mainBundle] pathForResource:@"point1sec" ofType:@"mp3"];
NSURL* soundfileURL = [NSURL fileURLWithPath:soundfilePath];
AVAudioPlayer* player = [[[AVAudioPlayer alloc] initWithContentsOfURL:soundfileURL error:&error] autorelease];
[player play];
You can create your own blank mp3 if you want, or do a google search on "blank mp3s" to find and download one already constructed by somebody else.