Ali Bdeir
Ali Bdeir

Reputation: 4365

Android - Click sound onClick of a button stops after rapidly clicking

I've seen this post, however nothing is working. Here's my current code:

private static MediaPlayer mp;
public void onClick(View v) {
    mp = new MediaPlayer();
    try {
        mp.reset();
        AssetFileDescriptor afd;
        afd = getAssets().openFd("click.mp3");
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        mp.prepare();
        mp.start();
    } catch (IllegalArgumentException | IllegalStateException | IOException e) {
        e.printStackTrace();
    }
    increment(button, key, this);
}

When I rapidly click on the button, the sound stops

Upvotes: 2

Views: 82

Answers (1)

Yogesh Rathi
Yogesh Rathi

Reputation: 6499

To fix it, you just have to add:

if(mediaPlayer == null)

before you create a new MediaPlayer. So, in your case:

if(mediaPlayer == null)
   mediaPlayer = new MediaPlayer();

And make sure mediaPlayer is a global variable.

Upvotes: 1

Related Questions