Playing a short sound in quick succession using MediaPlayer

Viewed 64

I have an app where I have to make a sound fairly often in succession. I'm using MediaPlayer, but keep running into these issues.

I'm initializing the MediaPlayer like this, which doesn't seem to be a problem:

MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.mysound);

At first, I simply tried calling mp.start(); whenever I need to play the sound. However, if I do that and try playing it in quick succession (via a button), it only plays it every 3-4 tries when it needs to be every time. Since there was no error, I figured it was already "playing". So I added code to check if it was playing and if so, called mp.stop() before calling start() again.

if (mp.isPlaying()) {
  mp.stop();
}
mp.start();

But then I get an error, start called in state #. So then I tried adding mp.setOnPreparedListener(this), overriding onPrepared and calling mp.prepareAsync() before calling mp.start(). However I keep getting errors about start called in state #.

Any suggestions on how to fix this? Or is there a better way?

0 Answers
Related