我们可以试想ImageView能显示图片,而VideoView就是用来显示视频的。
使用VideoView播放视频的步骤如下
【1】在界面布局中定义VideoView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > < VideoView android:id = "@+id/videoview" android:layout_width = "match_parent" android:layout_height = "0dp" android:layout_weight = "1" /> < Button android:id = "@+id/button" android:text = "播放" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </ LinearLayout > |
【2】调用如下两个方法加载指定视频
setVideoPath(String Path);加载路径下的视频
setVideoURL(URL url);加载url所对应的视频。
1
|
mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+ "/aa.mp4" ); |
【3】权限
1
|
<uses-permission android:name= "android.permission.READ_EXTERNAL_STORAGE" /> |
【4】调用
start()、stop()、pause()控制播放
【5】实际中常常结合MediaController类,它提供一个友好的图像控制界面控制视频播放;
1
|
mVideoView.setMediaController( new MediaController(MainActivity. this )); |
完整程序代码如下
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
|
public class MainActivity extends Activity { private VideoView mVideoView; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mVideoView= (VideoView) findViewById(R.id.videoview); mButton= (Button) findViewById(R.id.button); mButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //得到sdcard下面aa.mp4的視頻文件 //两种调用方式 // File videofile =new File("/mut/extSdCard/DCIM/Camera/20150915_160202.mp4"); // mVideoView.setVideoPath(videofile.getAbsolutePath()); mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+ "/20150915_160202.mp4" ); mVideoView.setMediaController( new MediaController(MainActivity. this )); mVideoView.start(); } }); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/TaooLee/article/details/48540793