AbAppletic AbAppletic - 3 months ago 9
Android Question

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

Answer
public void onClick(View v) {
   if(mp==null){
    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);}}
Comments