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);
}
});
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:
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 thresholdPass this weight to the overridden methods
onSwipeTop()
who must then use this value along with the maximum value from theAudioManager
see official android docs to adjust the volume (i.e. you may be doing +1, +2 etc)setStreamVolume
. I see that you are always using0
which is equivalent toADJUST_SAME
. You may consider incorporatingADJUST_RAISE
andADJUST_LOWER
respectively.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.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.Changing the volume based on previously calculated weight
NB. You would have to update the signatures and overrides for
onSwipeBottom
andonSwipeTop
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).
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.