Play resource files in App using VideoView

  • 2021-12-04 19:48:18
  • OfStack

This article example for everyone to share the use of VideoView to play App resource files in the specific code, for your reference, the specific content is as follows

Layout file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <VideoView
    android:id="@+id/vv_video"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

Create a new folder under res, raw, and then copy the video under that folder.

Specific implementation code


public class VideoActivity extends Activity{
  VideoView videoView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);
    initView();
  }

  public void initView(){
    videoView= (VideoView) findViewById(R.id.vv_video);
    playVideo();
  }
  public void playVideo(){
    //String file=Environment.getExternalStorageDirectory().getPath()+"/oppo.3gp";//oppo.3gp Name of video playback 
    String uri = "android.resource://" + getPackageName() + "/" + R.raw.oppo;
    videoView.setVideoURI(Uri.parse(uri));
    MediaController mc = new MediaController(this);
    // Set up the controller   What controls is that 1 A videoview
    mc.setAnchorView(videoView);
    // Settings videoview The controller of is mc
    videoView.setMediaController(mc);
    videoView.start();
  }
}

Related articles: