Right now I have two buttons. Each one needs to produce a different sound. In the future, there will probably be about 8 buttons, but for now just two.
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button btnDrum1 = (Button) findViewById(R.id.btnDrum1);
btnDrum1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.drum1);
mp.start();
mp.release();
}
});
final Button btnCym1 = (Button) findViewById(R.id.btnCym1);
btnCym1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.cym1);
mp.start();
mp.release();
}
});
}
}
Originally i didn't have mp.release() and it would play the sound properly, but eventually the app would crash due to running out of memory. Now with the mp.release() it doesn't crash, but sometimes it doesn't play the sound when clicked.
Is this the most efficeient way to play a sound when button is clicked? Is it extensible?