views:

1538

answers:

3

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:

NSString *audioFile = [NSString stringWithFormat:@"%@/%@.caf", [[NSBundle mainBundle] resourcePath], @"audiofile"];
NSData *audioData = [NSData dataWithContentsOfMappedFile:audioFile];

NSError *err;
AVAudioPlayer *audioPlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithData:audioData error:&err];

audioPlayer.delegate = self;
[audioPlayer play];

I also implement the audioPlayerDidFinishPlaying method to release the AVAudioPlayer once I'm done.

The first time I play the audio the lag is palpable -- at least 2 seconds. However, after that the sound plays immediately. I suspect that the culprit, then, is the [NSData dataWithContentsOfMappedFile] taking a long time reading from the flash initially, but then being fast on later reads. I'm not sure how to test that, though.

Is that the case? If so, should I just pre-cache the NSData objects and be aggressive about clearing them in low memory conditions?

Update:

The delay seems to be related to instantiating AVAudioPlayer for the first time. If I load any audio, run [audioPlayer prepareToPlay] and then immediately release it, the load times for all of my other audio is very close to imperceptible. So now I'm doing that in applicationDidFinishLaunching and everything else runs well.

I can't find anything about this in the docs, but it certainly seems to be the case.

A: 

I don't know for sure, but I suspect the NSData object is being lazy and loading the contents of the file on demand. You can try "cheating" by calling [audioData getBytes:someBuffer length:1]; at some early point to get it to load that file before it's needed.

n8gray
+3  A: 

The delay seems to be related to instantiating AVAudioPlayer for the first time. If I load any audio, run [audioPlayer prepareToPlay] and then immediately release it, the load times for all of my other audio is very close to imperceptible. So now I'm doing that in applicationDidFinishLaunching and everything else runs well.

I can't find anything about this in the docs, but it certainly seems to be the case.

John Biesnecker
A: 

This seems to be a very old thread, but for anyone else that stumbles upon it the answer is AVAudioPlayer *audioplayer; [audioplayer prepareToPlay];

Ben
Actually, [audioplayer prepareToPlay] isn't the answer... the delay I was talking about happens the first time you alloc and init an AVAudioPlayer object, before you call any of its methods. It only happens the very first time you load one, though. prepareToPlay just preloads the audio, but doesn't eliminate the lag I was actually asking about. Incidentally, UIWebView exhibits the same behavior -- if you alloc and then immediately release one, the rest load very quickly.
John Biesnecker