How to resume video where it last stopped using VideoView on Android?

Viewed 217

I'm trying to make a video player using VideoView. The video can play, but when I close the app and re-open it, the video doesn't resume from where it stopped and starts from the beginning. Does anyone know how to fix this? Thank you!

This is the code I'm using:

package com.example.praythisworks;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.MediaController;
import android.widget.VideoView;
import android.net.Uri;

public class MainActivity extends AppCompatActivity {

    static final String STATE_USER = "user";
    private String mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        VideoView videoView =(VideoView)findViewById(R.id.vdVw);
        //Set MediaController  to enable play, pause, forward, etc options.
        MediaController mediaController= new MediaController(this);
        mediaController.setAnchorView(videoView);
        //Location of Media File
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.thefeels);
        //Starting VideView By Setting MediaController and URI
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
        videoView.start();
    }

    
}
1 Answers

You can get the current progress from the VideoView via videoView.getCurrentPosition();

So before pausing get the current position of the video

int seekPosition = videoView.getCurrentPosition();

while resuming the video you can call seekTo() method on VideoView and start it like below.

videoView.seekTo(seekPosition);
videoView.start();
Related