Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop a sound while a button is pressed

I'm trying to loop a sound while a button is pressed down.

How could I do that while I'm pressing the button hadling the event "MotionEvent.ACTION_DOWN" will not stop running the sound? The way I have done it only runs once. I've tryied to change the return statement to false, but it doesn't works.

The setLoop MediaPlayer option does not help me because I want to continue playing new sounds although the current sound doesn't finished playing.

Here's my code:

public boolean onClick(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        //here should not stop running the soundPool
        soundPool.play(R.raw.sound1, 1, 1, 1, 0, 1);
    }
    if(event.getAction() == MotionEvent.ACTION_UP){
        // stop running the soundpool
    }
    return true;
}
like image 769
karse23 Avatar asked Mar 11 '26 05:03

karse23


1 Answers

Replace the fifth argument to -1 :

soundPool.play(R.raw.sound1, 1, 1, 1, -1, 1);

As of the play() doc the fifth argument loop takes either 0 ( = no loop), or -1 ( = loop forever)

like image 167
iTurki Avatar answered Mar 12 '26 17:03

iTurki