android----播放视频
播放适配文件主要是使用VideoView类来实现的,这个类将视频的显示和控制集于一身,使得我们仅仅借助它就可以完成一个简单的视频播放器,VideoView的用法和MediaPlayer也比较类似,主要有以下常用方法:
1 setVideoPath() //设置要播放视频文件的位置 2 start() //开始或继续播放视频 3 pause() //暂停播放视频 4 resume() //将视频从头开始播放 5 seekTo() //从指定位置开始播放视频 6 isPlaying() //判断当前是否在播放视频 7 getDuration() //获取载入的视频文件的时长
新建PlayVideoTest项目,然后修改activity_main.xml代码:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <LinearLayout 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content"> 8 9 <Button 10 android:id="@+id/play" 11 android:layout_width="0dp" 12 android:layout_height="wrap_content" 13 android:layout_weight="1" 14 android:text="Play" /> 15 <Button 16 android:id="@+id/pause" 17 android:layout_width="0dp" 18 android:layout_height="wrap_content" 19 android:layout_weight="1" 20 android:text="Pause"/> 21 <Button 22 android:id="@+id/replay" 23 android:layout_width="0dp" 24 android:layout_height="wrap_content" 25 android:layout_weight="1" 26 android:text="Replay"/> 27 </LinearLayout> 28 <VideoView 29 android:id="@+id/video_view" 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" /> 32 </LinearLayout>
在这个布局中,首先放置了3个按钮,分别用于控制视频的播放,暂停和重新播放,然后在按钮下面由放置了一个VideoView来显示视频
修改MainActivity代码
1 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
2
3 private VideoView videoView;
4
5 @Override
6 protected void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.activity_main);
9 videoView = (VideoView)findViewById(R.id.video_view);
10 Button play = (Button)findViewById(R.id.play);
11 Button pause = (Button)findViewById(R.id.pause);
12 Button replay = (Button)findViewById(R.id.replay);
13 play.setOnClickListener(this);
14 pause.setOnClickListener(this);
15 replay.setOnClickListener(this);
16 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
17 ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
18 } else {
19 initVideoPath();
20 }
21 }
22
23 private void initVideoPath(){
24 File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
25 videoView.setVideoPath(file.getPath());//指定视频文件路径
26 }
27
28 @Override
29 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
30 switch (requestCode){
31 case 1:
32 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
33 initVideoPath();
34 } else {
35 Toast.makeText(this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();
36 finish();
37 }
38 break;
39 default:
40 break;
41 }
42 }
43
44 @Override
45 public void onClick(View view) {
46 switch (view.getId()){
47 case R.id.play:
48 if (!videoView.isPlaying()){
49 videoView.start();//开始播放
50 }
51 break;
52 case R.id.pause:
53 if (videoView.isPlaying()){
54 videoView.pause();//暂停播放
55 }
56 break;
57 case R.id.replay:
58 if (videoView.isPlaying()){
59 videoView.resume();//重新播放
60 }
61 break;
62 }
63 }
64
65 @Override
66 protected void onDestroy() {
67 super.onDestroy();
68 if (videoView != null){
69 videoView.suspend();
70 }
71 }
72 }
首先在onCreate()方法中同样进行了一个运行时权限处理,因为视频文件将会在SD卡上,当用户同意授权之后就会调用initVideoPath()方法来设置视频文件的路径,我们需要事先在SD卡的根目录下放置一个名为movie.mp4的视频文件.
当点击Play按钮时会进行判断, 如果当前没有播放视频,则调用start()方法开始播放,当点击Pause按钮时会进行判断,如果当前视频正在播放,则调用pause()方法暂停播放,当点击Replay按钮时会进行判断,如果当前视频正在播放,则调用resume()方法从头播放视频.
最后在onDestroy()方法中,我们还需要调用一下suspend()方法,将VideoView占用的资源释放掉.
另外,要记得在AndroidManifest.xml中声明权限:
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.playaudiotest"> 3 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 4 ... 5 </manifest>

浙公网安备 33010602011771号