I'm making an application where there will be several buttons, each one will play a different audio. The reproduction of the audio works me the problem is that if you press the button again it does not stop, instead it plays it again without stopping the first playback, that is if I press 3 times it will sound 3 times the same audio. What I want to do is that by pressing the button again or by pressing a different one the one that is sounding will stop
public class TarjetaOberta extends AppCompatActivity {
Button btM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tarjeta_oberta);
references();
}
private void references(){
btM = (Button) findViewById(R.id.btMarta);
}
public void Marta(View view){
MediaPlayer ring= MediaPlayer.create(TarjetaOberta.this,R.raw.marta);
ring.start();
}
This is the code of the button that I have created.