Android - Looping Activity to Repeat MediaPlayer
- by Austin Anderson
I'm trying to create a soundboard for longer audio files and can't figure out how to stop an audio file and start it again without closing the activity. Let's say each audio file is one minute long. If I play the first audio file for 20 seconds and start the next audio file, the first stops playing and the second starts playing. However, if I click the first audio file again, the second stops playing and the first does not. I need help. This is driving me insane.
bAudio1 = (ImageButton) findViewById(R.id.bAudio1);
bAudio2 = (ImageButton) findViewById(R.id.bAudio2);
mpAudio1 = MediaPlayer.create(this, R.raw.audio1);
mpAudio2 = MediaPlayer.create(this, R.raw.audio2);
bAudio1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(mpAudio1.isPlaying()) {
mpAudio1.stop();
} else {
if(mpAudio2.isPlaying()) { mpAudio2.stop(); }
mpAudio1.start();
}
}
});
bAudio2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(mpAudio2.isPlaying()) {
mpAudio2.stop();
} else {
if(mpAudio1.isPlaying()) { mpAudio1.stop(); }
mpAudio2.start();
}
}
});
Thanks.