I have a sound with 7 seconds duration. I want to play it when pressing down and stop it after (for example 1 second) when pressing up. My code is like below, but I have a problem??? when pressing rapidly and several times, the sound play all 7 seconds and don't stop it. I want to create a simple piano. what's the problem? Do you have any idea for this?
c.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // Button Pressed
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;
}
});
During pressing down the button
first you need to stop the soundPool
if any present.
Write soundPool.stop(SID1_c);
in case MotionEvent.ACTION_DOWN:
before starting the soundPool
again.