Skip to content
Advertisement

How to move a VideoView to a specified duration?

I am trying to set a video playing in a VideoView to a specified position, but there’s no method like setCurrentPosition. What should I use?

Advertisement

Answer

Try using seekTo

private VideoView mVideoView;
private boolean mShouldStop = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mVideoView = (VideoView) findViewById(R.id.myvideoview);
        mVideoView.setVideoPath("/storage/emulated/0/Android/data/com.example.android.camera2video/files/a.mp4");
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus();
        mVideoView.seekTo(6000);
        mVideoView.start();
        trackProgress();
    }

    private void trackProgress() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!mShouldStop) {
                    if (mVideoView != null && mVideoView.isPlaying()) {
                        if (mVideoView.getCurrentPosition() >= 12000) {
                            mVideoView.stopPlayback();
                            mShouldStop = true;
                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement