0

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;
                        }
                    });

1 Answers1

0
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

ygngy
  • 3,630
  • 2
  • 18
  • 29
  • when I pressing down, the sound plays very well and when pressing up stop it. but when I pressing rapidly and several times it doesn't play my sound very good. I want to create a piano app by this ability that you can press key and play sound till you release it. – Mohamad Fattahi Mar 16 '18 at 18:53
  • 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 – ygngy Mar 16 '18 at 19:08
  • I try it but don't work. I put this code after 90); don't work and also I put it like in your code and unfortunately crash... Thank you very much. yes, I'm Iranian. – Mohamad Fattahi Mar 16 '18 at 19:45
  • you could add ( handler = new Handler() ) in beginning of onTouch event. what was exception message? – ygngy Mar 16 '18 at 19:59
  • when I pressing rapidly and several times it doesn't play my sound very good. Don't work – Mohamad Fattahi Mar 17 '18 at 04:49
  • when pressing button two times after releasing first press you start post delayed for stopping sound command for first release but this stopping command will hit second press not the first one to correct this you should stop handler when you play the sound – ygngy Mar 17 '18 at 08:51