On Swipe up Volume Is increasing but do not increase continious if i move finger up

1.1k views Asked by At

Actually i want to increase/decrease volume continuously, when i swipe finger up/down .I want when i move finger up slowly then volume increase with finger movement and decrease in finger down case. But my code increase volume when i swipe finger up only once. I need to again swipe up to increase more volume. Its not continuing increase as finger moves.

Please help me how i can do that. please check my code what is mistake. Please help thanks.

OnSwipeTouchListener.java (Class)

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener (Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                    result = true;
                }
            }
            else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffY > 0) {
                    onSwipeBottom();
                } else {
                    onSwipeTop();
                }
                result = true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }

}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}

}

VideoView.java (Activity)

 fullview = (View)findViewById(R.id.fullView);
        fullview.setOnTouchListener(new OnSwipeTouchListener(ViewVideo.this) {
            public void onSwipeTop() {
                Toast.makeText(ViewVideo.this, "top", Toast.LENGTH_SHORT).show();
                currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume + 1, 0);
            }
            public void onSwipeRight() {
                Toast.makeText(ViewVideo.this, "right", Toast.LENGTH_SHORT).show();
            }
            public void onSwipeLeft() {
                Toast.makeText(ViewVideo.this, "left", Toast.LENGTH_SHORT).show();
            }
            public void onSwipeBottom() {
                Toast.makeText(ViewVideo.this, "bottom", Toast.LENGTH_SHORT).show();
                currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume - 1, 0);
            }

        });
1

There are 1 answers

6
ggordon On

What you need to implement is weighted thresholds. i.e. Currently you trigger the swipeUp volume by 1 whenever you detect that user is swiping up. What you are interested in is a bit more detailed, you would like to increase the volume more based on how much (i.e. theweight) the user has swiped up.

The code is good thus far and a few modifications can be used to achieve this. Notably:

  1. in your onScroll or in a a helper method to determine how much/weight that the user has swiped up by and not just whether they have passed the threshold

  2. Pass this weight to the overridden methods onSwipeTop() who must then use this value along with the maximum value from the AudioManager see official android docs to adjust the volume (i.e. you may be doing +1, +2 etc)

  3. The api docs does document how to incorporate flags with setStreamVolume. I see that you are always using 0 which is equivalent to ADJUST_SAME . You may consider incorporating ADJUST_RAISE and ADJUST_LOWER respectively.
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume - 1, 0);

Determining the "weight" (how much to adjust volume by)

You could use several approaches to determine the weighting. These examples assume that you previously define SWIPE_VERTICAL_INCREMENT as the integer increment value to adjust your weighting by. This value can be present by you to small integers or you may opt to determine it based on the screen size of the device (good for rotational experiences read more...).

Approach 1

One approach is after you've detected that they have passed the SWIPE_THRESHOLD you could use the difference between the threshold and how much they have scrolled to determine the weight.

if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
               int swipeVerticalWeighting = (int)((Math.abs(diffY) - SWIPE_THRESHOLD) / SWIPE_VERTICAL_INCREMENT);
                if (diffY > 0) {
                    onSwipeBottom(swipeVerticalWeighting );
                } else {
                    onSwipeTop(swipeVerticalWeighting );
                }
                result = true;
}

Approach 2

Another approach is after you've detected that they have passed the SWIPE_THRESHOLD you could use the difference between how much they have scrolled to determine the weight.

if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
               int swipeVerticalWeighting = (int)(Math.abs(diffY) / SWIPE_VERTICAL_INCREMENT);
                if (diffY > 0) {
                    onSwipeBottom(swipeVerticalWeighting );
                } else {
                    onSwipeTop(swipeVerticalWeighting );
                }
                result = true;
}

Changing the volume based on previously calculated weight

NB. You would have to update the signatures and overrides for onSwipeBottom and onSwipeTop to accommodate this .

Finally you can adjust the volume based on weighting. Based on volume change there may be SecurityException or others and the "safe max volume " which prohibits some apps from exceeding a volume threshold without the user permission should be taken into consideration (this is configurable also).

public void onSwipeTop(int increaseVolumeBy) {
                Toast.makeText(ViewVideo.this, "top", Toast.LENGTH_SHORT).show();
                currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                //feel free to retrieve the max volume elsewhere, it should not change frequently
               int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//ensure volume being increased by is not more than the max volume
              int nextVolume = currentVolume + increaseVolumeBy;
              if(nextVolume > maxVolume){

                 nextVolume = maxVolume;

              }
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, nextVolume, AudioManager.ADJUST_RAISE);
}

Overall you need to decide how much you would like volume to be increased by based on how you swipe up and how much weighting that should be given. This can vary based on the type of app your delivering and your expected user based.