一、使用VideoView
1. 1、把视频文件放到项目工程的 res/raw 文件下。
1.2、视频文件必须是Android支持视频格式(3gp, wmv, mp4)
二、代码
2.1、activity_video.xml
<?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"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="1200dp" android:layout_gravity="center"/> </LinearLayout>
2.2、VideoPlayer.java
package com.gatsby.test; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.VideoView; import androidx.appcompat.app.AppCompatActivity; public class VideoPlayer extends AppCompatActivity { VideoView videoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video); videoView = (VideoView) findViewById(R.id.videoView); videoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/raw/test")); videoView.start(); } }