VideoView Realizes Seamless and Continuous Video Play

  • 2021-09-16 07:57:34
  • OfStack

The video player adopts a relatively simple videoview, and the basic idea is to monitor the video broadcast with setOnCompletionListener. When the video is played, the next video is played, and the video source is placed in the Download folder in the SD card. The specific code is as follows:


public class MainActivity extends Activity {
  VideoView videoView;
  static int pos=1; // Static integer variables are used to mark which video is played to 
  MediaController mController;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.activity_main);
    videoView=(VideoView)findViewById(R.id.video);
    mController=new MediaController(this);

    File video=new File("/mnt/sdcard/Download/video"+pos+".mp4");


    if(video.exists())
    {

      videoView.setVideoPath(video.getAbsolutePath());
    /* videoView.setMediaController(mController);
      mController.setMediaPlayer(videoView);*/
      videoView.requestFocus();
      videoView.start();
    }

    videoView.setOnCompletionListener(new OnCompletionListener() {

      @Override
      public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        pos=pos+1; // Here, change the video address to the following 1 A 
        File video2=new File("/mnt/sdcard/Download/video"+pos+".mp4");
        if(video2.exists())
        {
        try {

          videoView.setVideoPath(video2.getAbsolutePath());
          /*videoView.setMediaController(mController);
          mController.setMediaPlayer(videoView);*/
          videoView.requestFocus();
          videoView.start();

        } catch (Exception e) {
          e.printStackTrace();

        }
        }
        else
        {Toast.makeText(MainActivity.this," Video play finished ",Toast.LENGTH_SHORT).show();}


      }

    });
  }


}

Related articles: