delayed audio in android
<p>i'm trying to play audio when a button is clicked but there's some delay between when i press the button and when the audio is actually played. I get this error: <code>AudioFlinger(17396): write blocked for 162 msecs, 3 delayed writes, thread 0x15440</code> This is what i got so far: </p>

<pre><code>ImageButton i = (ImageButton)findViewById(R.id.button);
        i.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    s = new AudioPlayer(getApplicationContext());
                    s.playSound(R.raw.conga1);
            }
            return true;
            }
        });
</code></pre>

<p><strong>AudioPlayer class</strong></p>

<pre><code>public class AudioPlayer {

  private MediaPlayer mediaPlayer;
  private final OnCompletionListener mediaPlayerListener = new MediaPlayerListener();
  private Context context = null;

  public AudioPlayer(Context context)
  {
    this.context = context;
    init();
  }

  private void init() {
    if (mediaPlayer == null) {
      AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      int streamVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
      mediaPlayer = new MediaPlayer();
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mediaPlayer.setOnCompletionListener(mediaPlayerListener);
      mediaPlayer.setVolume(streamVolume, streamVolume);
    }
  }

  private void setSound(int id) {
    if (mediaPlayer!=null) {
      mediaPlayer.reset();
      AssetFileDescriptor file = context.getResources().openRawResourceFd(id);
      try {
        mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
        file.close();
        mediaPlayer.prepare();
      } catch (IOException e) {
        mediaPlayer = null;
      }
    }
  }

  public void playSound(int id) {
    if (mediaPlayer!=null) {
      setSound(id);
      mediaPlayer.start();
    }
  }

  private static class MediaPlayerListener implements OnCompletionListener {
    public void onCompletion(MediaPlayer mediaPlayer) {
      mediaPlayer.seekTo(0);
    }
  }
}
</code></pre>

<p>Any clues? </p>

<p>Thanks in advance.</p>

<p><strong>EDITS:</strong>
So i added a singleton and it helped but there's still delay. This is how it looks like now:</p>

<p><strong>AudioPlayer:</strong></p>

<pre><code>public static synchronized AudioPlayer getSingletonObject(Context context, Uri pathToFile) {
    if (audioPlayer == null) {
        audioPlayer = new AudioPlayer(context, pathToFile);
    }
    return audioPlayer;
}

public void setSound(String pathToFile) {
    if (mediaPlayer!=null) {
        mediaPlayer.reset();

        try {
            mediaPlayer.setDataSource(pathToFile);
            mediaPlayer.prepareAsync();
        } //catch....
    }
}

public void playSound(String path) {
    if (mediaPlayer!=null) {
        mediaPlayer.reset();

        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepareAsync();
            mediaPlayer.start();
        } //catch...
    }
}
</code></pre>

<p><strong>Main:</strong></p>

<pre><code>final String path = "sdcard/myappsounds/snaredrum2.wav";
    final AudioPlayer s = AudioPlayer.getSingletonObject(getApplicationContext(), Uri.parse(path));
    s.setSound(path);

    ImageButton i = (ImageButton)findViewById(R.id.button);
    i.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN){

                s.playSound(path);
        }
        return true;
        }
    });
</code></pre>

<p>Any ideas? What i'm trying to do is like a drumset, that's why the buttons must respond correctly. </p>
