简介
使用 VideoView 实现对视频的 播放 暂停 重新开始 功能
VideoView
VideoView 可以加载视频,但是当被切换到后台后,它不能自己去存储播放状态、播放位置以及一些设置,这些必须要 app 去自己管理。
关于 VideoView 更多
关键代码
// 添加布局文件
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
// 初始化
videoView = (VideoView) findViewById(R.id.video_view);
File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
videoView.setVideoPath(file.getPath());
// 在 onDestroy 中调用 suspend 释放资源
protected void onDestroy() {
super.onDestroy();
if(videoView != null) {
videoView.suspend();
}
}