i have this two button.as i press the first it plays an mp3 file.but if i press the second and the first mp3 hasnt finished yet,they play both together.how could i fix it??this is my btn code!!thanks
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
MediaPlayer mp = MediaPlayer.create(this, R.raw.myalo);
mp.start();
Toast.makeText(this, "Eisai sto myalo", Toast.LENGTH_SHORT).show();
}
});
Button button2 = (Button) findViewById(R.id.btn2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
MediaPlayer mp = MediaPlayer.create(this, R.raw.thryleole);
mp.start();
Toast.makeText(this, "thryle ole trelenomai", Toast.LENGTH_SHORT).show();
}
You can achieve this easily by checking to see if the MediaPlayer is playing before you start it. You’d want to do something like this:
This will do nothing if the user clicks the button while the MediaPlayer is still playing so it will continue with whatever it was playing. You could instead call mp.stop() and start the new sound file. That way instead of ignoring the click it would stop the current sound no matter how far into it we are and start the new one.