How can I apply a scroll/fling gesture to a VideoView

Viewed 44

I'm trying to make a scrollable VideoView. I want the scroll gesture of the user to make the videoview progress smoothly. I hope that makes sense.

I've tried to apply an OnTouchListener to the VideoView which then gives the MotionEvent to a GestureDetector and then I've tried overriding the OnScroll method of said GestureDetector. The video progresses but very abruptly and at unregular intervals.

I would be very thankful for any pointer that you could give me. I'm quite new to programming as you can probably tell so don't hesitate to dumb it down.

Here's my code so far, thank you in advance.

public class MainActivity extends AppCompatActivity implements
    View.OnTouchListener,
    GestureDetector.OnGestureListener {

    GestureDetector mgestureDetector;
    VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mgestureDetector = new GestureDetector(this, this);
        videoView = findViewById(R.id.video_view);
        String videoPath = "android.resource://" +getPackageName() +"/" + R.raw.video;
        Uri uri = Uri.parse(videoPath);
        videoView.setVideoURI(uri);
        videoView.setOnTouchListener(this);
    }

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

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

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

        videoView.seekTo(videoView.getCurrentPosition() + (int)distanceY);
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
}
0 Answers
Related