Search code examples
androidontouchlistener

Gesture- volume control


i am trying to implement volume control using onTouchListener. which i did nicely, but the problem is the volume gets increased by just 1 step.

For example: Total volume is 100 and steps are 10,20,30,40.. so on, it will only increase it 10 --NO MATTER HOW MANY TIMES I SWIPE THE LAYOUT UP--

but the volume down is super INSTANT, slight touch --GOES TO ZERO LIKE BANANAS--

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP)
        Log.i(TAG, "Action :" + event.getAction() + "\t X :" + event.getRawX() + "\t Y :"+ event.getRawY());

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:


            downY = event.getY();

        case MotionEvent.ACTION_UP:

            downY = event.getY();

        case MotionEvent.ACTION_MOVE:


            float y2 = event.getY();

            diffY = (long) (Math.ceil(event.getY() - downY));

            //if (Math.abs(diffY) > Math.abs(diffX)) {

                if (downY < y2) {

                    //down swipe volume decrease
           //         audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
                } else if (downY > y2) {

                    //up  swipe volume increase
              //      audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
                }
          //  }
       }

    return true;

}

Solution

  • I fixed it changing setStreamVolume to adjustStreamVolume.