How to detect when a VideoView is paused in Android

In this android programming source code example, we are going to detect when a VideoView is paused in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to detect when a VideoView is paused in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.bluapp.androidview.VideoView.StateBroadcastingVideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

MainActivity.java

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoViewActivity16 extends AppCompatActivity {
    private StateBroadcastingVideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_view16);
        videoView = (StateBroadcastingVideoView)findViewById(R.id.videoView);
        videoView.setMediaController(new MediaController(this));
        videoView.setPlayPauseListener(new StateBroadcastingVideoView.PlayPauseListener() {
            @Override
            public void onPlay() {

            }

            @Override
            public void onPause() {
                Toast.makeText(VideoViewActivity16.this, "Video pause", Toast.LENGTH_SHORT).show();
            }
        });
        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.test);
        videoView.setVideoURI(uri);
        videoView.start();
    }
}

StateBroadcastingVideoView.java

import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;

public class StateBroadcastingVideoView extends VideoView{
    public interface PlayPauseListener {
        void onPlay();
        void onPause();
    }

    private PlayPauseListener mListener;

    public StateBroadcastingVideoView(Context context) {
        super(context);
    }

    public StateBroadcastingVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StateBroadcastingVideoView(Context context, AttributeSet attrs, int theme) {
        super(context, attrs, theme);
    }

    @Override
    public void pause() {
        super.pause();
        if(mListener != null) {
            mListener.onPause();
        }
    }

    @Override
    public void start() {
        super.start();
        if(mListener != null) {
            mListener.onPlay();
        }
    }

    public void setPlayPauseListener(PlayPauseListener listener) {
        mListener = listener;
    }

}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment