Search code examples
androidtimeandroid-videoviewmediacontrollerpostdelayed

How to pause Android VideoView after 4 seconds of playing


I Want to pause my video play after 4 seconds of playing, then I want to pop up a message to the user. Can someone point me in the right direct how to pause the video after 4 seconds please. Thanks

V2 Implemented @gogothebee feedback {Now the video pause at position 0 (before it actually start).

public class PlayVideoActivity extends ActionBarActivity {

    public static final String TAG = PlayVideoActivity.class.getSimpleName();
    Bundle bunde;
    Intent intent;
    String content_actual_content;

    VarController vc = new VarController(PlayVideoActivity.this);
    public ImageLoader imageLoader;

    private VideoView mVideoView;
    private MediaController mMediaController;
    ProgressBar pbplay1;

    private boolean isActive;
    private Handler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video); //playvideo (FullScreen)

        handler = new Handler();

        //Hide Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        //Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();

        //Set ProgressBar
        pbplay1 = (ProgressBar) findViewById(R.id.pbplay1);

        //Get Link from ImageAdapter
        intent = this.getIntent();
        bunde = intent.getExtras(); 
        content_actual_content = bunde.getString("content_actual_content");

        Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();

        //Initiate Player
        mVideoView = (VideoView) findViewById(R.id.videoview);                      
        mVideoView.setVideoURI(Uri.parse(content_actual_content));
        mVideoView.setMediaController(new MediaController(PlayVideoActivity.this));
        mVideoView.requestFocus();

        mVideoView.setMediaController(mMediaController);

        //new myAsync().execute();

    }//--- end onCreate





    @Override
    protected void onPause() {
        super.onPause();
        isActive = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        isActive = true;
        mVideoView.requestFocus();
        mVideoView.start();
        scheduleVideoPause(4000);
    }

    private void scheduleVideoPause(int msec) {
        handler.removeCallbacksAndMessages(null);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!isActive) {
                    return;
                }
                mVideoView.pause();
                Toast.makeText(PlayVideoActivity.this, "Video has PAUSED at: " + mVideoView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
            }
        }, msec);
    }

V1

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video); //playvideo (FullScreen)

        //Hide Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        //Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();

        //Set ProgressBar
        pbplay1 = (ProgressBar) findViewById(R.id.pbplay1);

        //Get Link from ImageAdapter
        intent = this.getIntent();
        bunde = intent.getExtras(); 
        content_actual_content = bunde.getString("content_actual_content");

        Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();

        //Initiate Player
        mVideoView = (VideoView) findViewById(R.id.videoview);                      
        mVideoView.setVideoURI(Uri.parse(content_actual_content));
        mMediaController = new MediaController(PlayVideoActivity.this);
        mVideoView.setMediaController(mMediaController);
        mVideoView.requestFocus();

        mVideoView.start();
        //new myAsync().execute();


        Log.i("**********************", "getCurrentPosition: " + mVideoView.getCurrentPosition());

        if (mVideoView.getCurrentPosition() == 4000) {
            mVideoView.pause();
            Toast.makeText(PlayVideoActivity.this, "Video has PAUSED at: " + mVideoView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
        }





    }//--- end onCreate

Solution

  • It seems that there's no way to do it easily. However, my solution is to spawn a background timer that runs every 100ms (adjustable) and check the current position. Once it is past the desired threshold (4000ms in this case), the video is stopped. I know it's not elegant and won't work exactly at 4000ms, but if anyone has a better idea, please share it. When adjusting the pooling interval in case you want to get as close as possible to 4000ms, don't go too low for performance reasons.

    This solution can also be refactored as it's quite messy, but I wanted to show the concept in as little code as possible.

    private Timer timer;
    private boolean isActive;
    private VideoView mVideoView;
    private final static int POOLING_INTERVAL_MS = 100;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Initiate Player
        mVideoView = (VideoView) findViewById(R.id.videoview);
        mVideoView.setVideoURI(Uri.parse("Your-Video-URI"));
        mVideoView.setMediaController(new MediaController(this));
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        isActive = false;
        cancelProgressPooling();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        isActive = true;
        mVideoView.requestFocus();
        mVideoView.start();
        initVideoProgressPooling(4000);
    }
    
    private void initVideoProgressPooling(final int stopAtMsec) {
        cancelProgressPooling();
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mVideoView.post(new Runnable() {
                    @Override
                    public void run() {
                        if (!isActive) {
                            cancelProgressPooling();
                            return;
                        }
                        if(mVideoView.getCurrentPosition() >= stopAtMsec) {
                            mVideoView.pause();
                            cancelProgressPooling();
                            Toast.makeText(MainActivity.this, "Video has PAUSED at: " + mVideoView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }, 0, POOLING_INTERVAL_MS);
    }
    
    private void cancelProgressPooling() {
        if(timer != null) {
            timer.cancel();
        }
        timer = null;
    }