SoFunction
Updated on 2025-04-09

5 steps to learn to play videos using VideoView

We can imagine that ImageView can display images, and VideoView is used to display videos.

The steps to play videos using VideoView are as follows

【1】Define VideoView in interface layout

<LinearLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity">

  <VideoView
    android:
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>
  <Button
    android:
    android:text="Play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

【2】Call the following two methods to load the specified video

setVideoPath (String Path); video under load path
setVideoURL(URL url); loads the video corresponding to the url.

(()+"/aa.mp4");

【3】Permissions

<uses-permission android:name=".READ_EXTERNAL_STORAGE"/>

【4】Call

start(), stop(), pause() control playback

【5】In practice, the MediaController class is often combined, which provides a friendly image control interface to control video playback;

(new MediaController());

The complete program code is as follows

public class MainActivity extends Activity {

  private VideoView mVideoView;
  private Button mButton;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);

    mVideoView= (VideoView) findViewById();
    mButton= (Button) findViewById();
    (new () {
      @Override
      public void onClick(View v) {

        //Get the video file of aa.mp4 below sdcard        //Two ways of calling//        File videofile =new File("/mut/extSdCard/DCIM/Camera/20150915_160202.mp4");
//        (());
        (()+"/20150915_160202.mp4");
        (new MediaController());
        ();
      }
    });
  }


}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.