So, let’s continue with the logic of stopping a sound when is called a second sound from the soundboard. I will start directly by showing what you will need to write in the OnClickListener of every button, so you will not get a Force Close error.
ImageButton a=(ImageButton)findViewById(R.id.ImageButton01); ImageButton b=(ImageButton)findViewById(R.id.ImageButton02); a.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { if(mp==null){playSound(R.raw.sound1);}//if is not playin any sound, then we start this sound else{mp.release(); playSound(R.raw.sound1);} //else if there is a sound playing, we release it and then play the current sound }}); b.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { if(mp==null){playSound(R.raw.sound2);} else{ mp.release(); playSound(R.raw.sound2);} }});
Also, you can get an error when you press the hard key “back” on your phone, if the sound still playing. For this we override the finish() method like this:
@Override public void finish() { if(mp==null){} else{mp.release();} super.finish(); }