-2

How do i implement the swipe/touch volume and brightness adjustment functionality in the app.

I have Tried Playerview set on Touch Listener. ExoPlayer Set On Touch Listners. It On Touch Listeners Work Only if if Keep Swiping the View. I Need to implement Continuous swiping like the Functionalit in MX Player on Builtin Video Player App Of Any Android Mobile....

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Abdul Manan
  • 43
  • 1
  • 9

1 Answers1

4

Volume Control

add permission android.permission.MODIFY_AUDIO_SETTINGS to your AndroidManifest.xml

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.adjustStreamVolume(
    AudioManager.STREAM_MUSIC,           // or STREAM_ACCESSIBILITY, STREAM_ALARM, STREAM DTMF, STREAM_NOTIFCATION, STREAM_RING, STREAM_SYSTEM, STREAM_VOICE_CALL
    AudioManager.ADJUST_LOWER,           // or ADJUST_RAISE, ADJUST_SAME
    0                                    // or FLAG_PLAY_SOUND, FLAG_REMOVE_SOUND_AND_VIBRATE, FLAG_SHOW_UI, FLAG_VIBRATE, FLAG_ALLOW_RINGER_MODES
    ) 

Adjusts the volume of a particular stream by one step in a direction.

This method should only be used by applications that replace the platform-wide management of audio settings or the main telephony application.

This method has no effect if the device implements a fixed volume policy as indicated by isVolumeFixed().

From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed unless the app has been granted Do Not Disturb Access. See NotificationManager#isNotificationPolicyAccessGranted().

https://developer.android.com/reference/android/media/AudioManager

Brightness Control

add permission Manifest.permission.WRITE_SETTINGS to your AndroidManifest.xml

if (Settings.System.canWrite(getContext())) {
    // values between 0-255
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255); // full brightness
}

https://developer.android.com/reference/android/provider/Settings.System

Swiping:

credit to Mirek Rusin: Android: How to handle right to left swipe gestures

OnSwipeTouchListener.java:

import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

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 onFling(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() {
    }
}

Usage:

imageView.setOnTouchListener(new OnSwipeTouchListener(MyActivity.this) {
    public void onSwipeTop() {
        Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeRight() {
        Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeLeft() {
        Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeBottom() {
        Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
    }

});
Community
  • 1
  • 1
Software Person
  • 2,526
  • 13
  • 18
  • 1
    Controls Are Not The Issue Implementing them on a Continuous Touch is The Real Problem.....For Example, If i Swipe left to Right Volume control Should adjust Volume. If I Swipe from bottom to Top the screen Brightness control Should be Called... – Abdul Manan Jun 27 '19 at 09:44