Android 播放视频

思路

播放视频一般使用VideoView类实现:

  • setVideoPath:选择要播放的视频文件
  • start():开始或继续播放视频
  • pause():暂停播放
  • resume():重头播放
  • seekTo():跳转到指定时间
  • isPlaying():判断当前播放状态
  • getDuration():获取视频时长
Demo Code:
主布局:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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="播放" />

<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停" />

<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重播" />

</LinearLayout>

<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
主活动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private VideoView videoView;
private Button play;
private Button pause;
private Button replay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

videoView = findViewById(R.id.video_view);
play = findViewById(R.id.play);
pause = findViewById(R.id.pause);
replay = findViewById(R.id.replay);

play.setOnClickListener(this);
pause.setOnClickListener(this);
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() {
File file = new File(Environment.getExternalStorageDirectory() + "/Download/", "qinshi.mp4");
Log.d("test", file.getPath());
videoView.setVideoPath(file.getPath());
}

@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:
videoView.resume();
break;
}

}

@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();
finish();
}
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (videoView != null) {
videoView.suspend();
}
}
}
添加权限
1
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

0%