I have a sound with 4 duration. I want to play continuously this sound by pressing down without stopping it, like piano keys when you pressing down, it plays a sound without looping or stopping.
c.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // Button Pressed
soundPool.stop(SID1_c);
SID1_c = soundPool.play(sound_c, 1, 1, 1, 0, 0);
c.setBackgroundResource(R.drawable.key4);
return true;
case MotionEvent.ACTION_UP:// Button released
handler =new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
soundPool.stop(SID1_c);
}
},90);
return false;
}
return false;
}
});
button.setOnTouchListener(new View.OnTouchListener(){
onTouch(View v, MotionEvent event){
switch(getActionMasked()){
case ACTION_BUTTON_PRESS:
handler.removeCallbacksAndMessages(null);
soundPool.stop(SID1_c);
SID1_c = soundPool.play(sound_c, 1, 1, 1, 0, 0);
c.setBackgroundResource(R.drawable.key4);
return true;
case ACTION_BUTTON_RELEASE:
handler.postDelayed(new Runnable() {
@Override
public void run() {
soundPool.stop(SID1_c);
}
},90);
return false;
}
return false;
}
});
your code problem:
if you tap button two times
when you start post delayed for stopping sound for first button up this stopping will hit second press not the first one which has been stopped before
to correct this you should exit handler when you play the sound
you can use handler.removeCallbacksAndMessages(null);
when a new press is done