Java – How do I add click-to-play/pause to VideoView?

How do I add click-to-play/pause to VideoView?… here is a solution to the problem.

How do I add click-to-play/pause to VideoView?

I’m trying to add the video play/pause feature to the chat I’m doing, so I have a standard:

VideoView vidRight;
vidRight = v.findViewById(R.id.videoViewRight);

However, trying to add a click to play/pause the listener is not possible because I need to declare paused final:

boolean paused = false;
vidRight.setOnTouchListener(new View.OnTouchListener() {

@Override
    public boolean onTouch(View view, MotionEvent event) {
        if (paused) {
            vidRight.start();
            paused = false;
        }
        else {
            vidRight.stopPlayback();
            paused = true;
        }

return true;
    }
});

Is there another way?

Solution

You do not have to maintain a custom flag paused to check the status. Instead, you can use isPlaying to check if the video is currently playing.

vidRight.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (vidRight.isPlaying()) {
            vidRight.stopPlayback();
         }
         else {
            vidRight.start();
         }

return true;
    }
});

Related Problems and Solutions