AndroidManifest.xml
添加权限,由于视频可以是由网络提供,所以这里要有网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions"></uses-permission>
VideoActivity.java
这里要说一下
Environment.getExternalStorageDirectory().getPath()路径指的是:/storage/emulated/0/
package com.example.a20200712;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class VideoActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
VideoView videoView = (VideoView)findViewById(R.id.video_layout_videoBox);
//加载指定的视频文件
//String path = Environment.getExternalStorageDirectory().getPath()+"/Download/haha.mp4";
//videoView.setVideoPath(path);
//本地数据
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/Download/haha.mp4");
//网络视频
//Uri uri = Uri.parse("http://poss.videocloud.cns.com.cn/oss/2020/07/19/chinanews/MEIZI_YUNSHI/onair/F1B171FB2ECB4319ADAC3FF2915C7E4B.mp4");
videoView.setVideoURI(uri);
//创建MediaController对象
MediaController mediaController = new MediaController(this);
//VideoView与MediaController建立关联
videoView.setMediaController(mediaController);
//让VideoView获取焦点
videoView.requestFocus();
//打开就立马播放
videoView.start();
}
}
video_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/video_layout_videoBox"
android:layout_width="match_parent"
android:layout_height="match_parent">
</VideoView>
</LinearLayout>