1.在布局中写上VideoView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.activity中代码
mMediaController = new MediaController(this);
//我把文件写到缓存中了,FileIOUtils是blankj的
//https://github.com/Blankj/AndroidUtilCode
String cacheVideoPath = new File(getCacheDir(), "boot_animation.mp4").getPath();
if (!FileUtils.isFileExists(cacheVideoPath)) {
try {
InputStream is = getResources().getAssets().open("boot_animation.mp4");//文件在assets下
FileIOUtils.writeFileFromIS(cacheVideoPath, is);
} catch (Exception e) {
e.printStackTrace();
}
}
videoView.setVideoPath(cacheVideoPath);
videoView.setMediaController(mMediaController);
videoView.seekTo(0);
videoView.requestFocus();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
doWork();//播放完我写了跳到其他页面
}
});
videoView.start();