0

im trying to do a Soundboard for a school proyect, but I have problems when I pulse the buttons, If I pulse a button twice, the sound does not reproduce again, and, even if the condition fullfills, the audio doesnt loop. I have different buttons, and all of they call a method (each one with different parameters on the call

btn1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                reproducir(mp1, motionEvent, btn1, colorin);
                return false;
            }

        });
public void reproducir(MediaPlayer mp, MotionEvent motionEvent, Button btn, String colorin ){
    switch(motionEvent.getAction()){
        case MotionEvent.ACTION_DOWN:
            mp.start();               
btn.setBackgroundColor(getResources().getColor(R.color.Blanco));
            if(instrumento.getText().equals("piano")) { 
                mp.setLooping(true);
            }
            break;
        case MotionEvent.ACTION_UP:
            mp.stop();

            ponerColor(colorin);
    }
}

The last line calls to a method to put again the original color of the button, thanks a lot

Ametz
  • 1
  • 1

1 Answers1

0

Your Code is Fine Only One tiny Mistake: You should replace mp.stop(); with mp.pause();

That's All.

Better to it Like this though:

if (mp.isPlaying()){
mp.pause();
}


....


if (!mp.isPlaying()){
mp.start();
}

...


if (!mp.isLooping()){
mp.setLooping(true);
}

I should also let you know that mp.stop() Stops the media, it's Good, But Stop is only to be used in case You want the Whole MediaPlayer Forget what song is being played and we just wanna change The Audio, reset MEdia player and start another Audio.

Hope you find this useful

Shahin
  • 391
  • 3
  • 9