title: Slow start for AVAudioPlayer the first time a sound is played
body: <p>I'm trying to eliminate startup lag when playing a (very short -- less than 2 seconds) audio file via AVAudioPlayer on the iPhone.</p>
  
  <p>First, the code:</p>
  
  <pre><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:&amp;err];
  
  audioPlayer.delegate = self;
  [audioPlayer play];
  </code></pre>
  
  <p>I also implement the audioPlayerDidFinishPlaying method to release the AVAudioPlayer once I'm done. </p>
  
  <p>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.</p>
  
  <p>Is that the case? If so, should I just pre-cache the NSData objects and be aggressive about clearing them in low memory conditions?</p>