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

this is my code for play video . i have one main video file 19 second length i want to play video from 6 second to 12 second i am able to start video from 6 second . i don't know how to stop video play on 12 sec please suggest me how implement this .

Adevelopment
  • 265
  • 3
  • 15

2 Answers2

0

Not tested this, but it should work in your case.

 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();
    }
kara4k
  • 407
  • 5
  • 11
0

Since the VideoView documentation doesn't have hooks for this, let's take a step back: what do we want to do?

  1. Play the video (already done)
  2. After x amount of time, stop the video (to do)

Implement a timer

We want to play a video for an amount of time, then stop the video. Why not implement a Timer (see documentation)? On this timer, you can schedule a task to be performed.

One caveat: Don't forget to cancel() the timer. Otherwise it'll keep calling the method to pause/stop the video.

Community
  • 1
  • 1
M. Mimpen
  • 1,212
  • 16
  • 21