When I use mediaplayer on click button, after several clicks the sound disappears. When I use soundpool, the time is too short to play the effect sound. Can anybody please help me with this?

1 Answers

0
Community On Best Solutions

As I understood your audio is 20 s. So you can use this code on your button click:

if (mediaPlayer != null){
    mediaPlayer.stop();
    mediaPlayer.release();
    mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
mediaPlayer.start();

The first if statement is to take care that it will keep playing how much ever you clicked the button.