title: MediaPlayer stutters at start of mp3 playback
body: <p>I've been having a problem playing an mp3 file stored in a raw resource: when the file first starts playing, it generates perhaps a quarter of a second of sound and then restarts. (I know that this is basically a duplicate of the problem described <a href="https://stackoverflow.com/questions/5142060/android-stuttered-audio">here</a>, but the solution offered there hasn't worked for me.) I have tried several things and have made some progress on the problem, but it isn't totally fixed.</p>
  
  <p>Here's how I'm setting up to play a file:</p>
  
  <pre><code>mPlayer.reset();
  try {
      AssetFileDescriptor afd = getResources().openRawResourceFd(mAudioId);
      if (afd == null) {
          Toast.makeText(mOwner, "Could not load sound.",
                  Toast.LENGTH_LONG).show();
          return;
      }
      mPlayer.setDataSource(afd.getFileDescriptor(),
              afd.getStartOffset(), afd.getLength());
      afd.close();
      mPlayer.prepare();
  } catch (Exception e) {
      Log.d(LOG_TAG, "Could not load sound.", e);
      Toast.makeText(mOwner, "Could not load sound.", Toast.LENGTH_LONG)
              .show();
  }
  </code></pre>
  
  <p>If I exit the activity (which calls <code>mPlayer.release()</code>) and come back to it (creating a new MediaPlayer), the stutter is usually (but not always) gone&mdash;<em>provided</em> I load the same sound file. I tried a couple of things that made no difference:</p>
  
  <ul>
  <li>Load the sound file as an asset instead of as a resource.</li>
  <li>Create the MediaPlayer using <code>MediaPlayer.create(getContext(), mAudioId)</code> and skip the calls to <code>setDataSource(...)</code> and <code>prepare()</code>.</li>
  </ul>
  
  <p>Then I noticed that LogCat always shows this line at about the time that playback starts:</p>
  
  <pre><code>DEBUG/AudioSink(37): bufferCount (4) is too small and increased to 12
  </code></pre>
  
  <p>It got me wondering if the stuttering is due to the apparent rebuffering. This led me to try something else:</p>
  
  <ul>
  <li>After calling <code>prepare()</code>, call <code>mPlayer.start()</code> and immediately call <code>mPlayer.pause()</code>.</li>
  </ul>
  
  <p>To my pleasant surprise, this had a big effect. A great deal of the stutter is gone, plus no sound (that I can hear) is actually played at that point in the process.</p>
  
  <p>However, it still stutters from time to time when I call <code>mPlayer.start()</code> for real. Plus, this seems like a huge kludge. Is there any way to kill this problem completely and cleanly?</p>
  
  <p><strong>EDIT</strong> More info; not sure if related. If I call <code>pause()</code> during playback, seek to an earlier position, and call <code>start()</code> again, I hear a short bit (~1/4 sec) of additional sound from where it was paused before it starts playing at the new position. This seems to point to more buffering problems.</p>
  
  <p>Also, the stuttering (and paused buffer) problems show up on emulators from 1.6 through 3.0.</p>