Alright so, I am making sound application for android and there is a slight problem. I made my app to stop playing current sound when I click on another button to play another sound, so they don't play on top of each other. For first 4 Media Players, it's working good. But there is a problem on 5th and 6th Media player and I can't figure out what's causing it. When I play Mp6, and I want to play for example Mp2, it pauses it first so I need to double click to play it. heres my code
mp=MediaPlayer.create(this, R.raw.hekler);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
if (mp2.isPlaying()){
mp2.pause();
mp2.seekTo(0);
}
if (mp3.isPlaying()){
mp3.pause();
mp3.seekTo(0);
}
if (mp4.isPlaying()){
mp4.pause();
mp4.seekTo(0);
}
if (mp5.isPlaying()){
mp5.pause();
mp5.seekTo(0);
}
if (mp6.isPlaying()){
mp6.pause();
mp6.seekTo(0);
}
else{
mp.start();
}
}
});
mp2=MediaPlayer.create(this, R.raw.famas);
ImageButton btn2 = (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
if (mp3.isPlaying()){
mp3.pause();
mp3.seekTo(0);
}
if (mp4.isPlaying()){
mp4.pause();
mp4.seekTo(0);
}
if (mp5.isPlaying()){
mp5.pause();
mp5.seekTo(0);
}
if (mp6.isPlaying()){
mp6.pause();
mp6.seekTo(0);
}
else{
mp2.start();
}
}
});
mp3=MediaPlayer.create(this, R.raw.kalas);
ImageButton btn3 = (ImageButton) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp3.isPlaying()){
mp3.pause();
mp3.seekTo(0);
}
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
if (mp2.isPlaying()){
mp2.pause();
mp2.seekTo(0);
}
if (mp4.isPlaying()){
mp4.pause();
mp4.seekTo(0);
}
if (mp5.isPlaying()){
mp5.pause();
mp5.seekTo(0);
}
if (mp6.isPlaying()){
mp6.pause();
mp6.seekTo(0);
}
else{
mp3.start();
}
}
});
mp4=MediaPlayer.create(this, R.raw.emka);
ImageButton btn4 = (ImageButton) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp4.isPlaying()){
mp4.pause();
mp4.seekTo(0);
}
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
if (mp2.isPlaying()){
mp2.pause();
mp2.seekTo(0);
}
if (mp3.isPlaying()){
mp3.pause();
mp3.seekTo(0);
}
if (mp5.isPlaying()){
mp5.pause();
mp5.seekTo(0);
}
if (mp6.isPlaying()){
mp6.pause();
mp6.seekTo(0);
}
else{
mp4.start();
}
}
});
mp5=MediaPlayer.create(this, R.raw.uzi);
ImageButton btn5 = (ImageButton) findViewById(R.id.btn5);
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
if (mp2.isPlaying()){
mp2.pause();
mp2.seekTo(0);
}
if (mp3.isPlaying()){
mp3.pause();
mp3.seekTo(0);
}
if (mp4.isPlaying()){
mp4.pause();
mp4.seekTo(0);
}
if (mp6.isPlaying()){
mp6.pause();
mp6.seekTo(0);
}
if (mp5.isPlaying()){
mp5.pause();
mp5.seekTo(0);
}
else{
mp5.start();
}
}
});
mp6=MediaPlayer.create(this, R.raw.sporet);
ImageButton btn6 = (ImageButton) findViewById(R.id.btn6);
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
if (mp2.isPlaying()){
mp2.pause();
mp2.seekTo(0);
}
if (mp3.isPlaying()){
mp3.pause();
mp3.seekTo(0);
}
if (mp4.isPlaying()){
mp4.pause();
mp4.seekTo(0);
}
if (mp5.isPlaying()){
mp5.pause();
mp5.seekTo(0);
}
if (mp6.isPlaying()){
mp6.pause();
mp6.seekTo(0);
}
else{
mp6.start();
}
}
});
}