这里我是用VideoView来进行播放视频
注意:请事先在手机的SD卡的根目录下放一个名为movie.mp4的视频文件,因为等下我就是读取这个文件的路径并进行播放,暂停和重新播放的操作。
首先我们在AndroidManifest.xml文件中添加以下内容:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
MainActivity的布局文件代码如下:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放"
android:textSize="20sp" />
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停"
android:textSize="20sp" />
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重新播放"
android:textSize="20sp" />
</LinearLayout>
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
布局很简单,就是三个按钮,分别控制播放,暂停和重新播放。
MainActivity的java代码如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_play, btn_pause, btn_replay;
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化组件
btn_play = findViewById(R.id.play);
btn_pause = findViewById(R.id.pause);
btn_replay = findViewById(R.id.replay);
videoView = findViewById(R.id.video_view);
// 添加点击事件
btn_play.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_replay.setOnClickListener(this);
// 判断是否授权,如果没有,就动态申请
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
initVideoPath();
}
}
private void initVideoPath() {
// 设置文件为SD卡根目录下的一个为movie.mp4的文件
File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
// 指定视频文件的路径
videoView.setVideoPath(file.getPath());
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initVideoPath();
} else {
Toast.makeText(this, "没有存储权限,请先开启权限", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!videoView.isPlaying()) {
videoView.start();//开始播放
}
break;
case R.id.pause:
if (videoView.isPlaying()) {
videoView.pause(); // 暂停播放
}
break;
case R.id.replay:
if (videoView.isPlaying()) {
videoView.resume();//重新播放
}
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (videoView != null) {
videoView.suspend();
}
}
}
这里除了动态申请权限以外,其它的都见名知义,非常简单,相信你都能看的懂。