I am trying to write an Android Soundboard with multiple buttons. Each button should play a different sound.
I have a OnClickListener on each button:
buttonAlan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
playSound("alan");
}
});
Each of these buttons is calling the following function.
private void playSound(String sound) {
int path = getResources().getIdentifier(sound, "raw", getPackageName());
mediaplayer = MediaPlayer.create(this, path);
try {
mediaplayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaplayer.start();
}
The soundboard works, but everytime you click a button it plays the sound over and over again. I need the mediaplayer to stop playing, but everytime I write something like mediaplayer.stop(), the App won't work at all. Any suggestions what I should change in my function/code?