5步学会使用VideoView播放视频
作者:TaooLee 时间:2023-09-12 05:51:07
我们可以试想ImageView能显示图片,而VideoView就是用来显示视频的。
使用VideoView播放视频的步骤如下
【1】在界面布局中定义VideoView
<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所对应的视频。
mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4");
【3】权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
【4】调用
start()、stop()、pause()控制播放
【5】实际中常常结合MediaController类,它提供一个友好的图像控制界面控制视频播放;
mVideoView.setMediaController(new MediaController(MainActivity.this));
完整程序代码如下
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
标签:VideoView,播放视频
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在启动后台 jar包时,使用指定的 application.yml操作
2023-01-08 20:10:12
详解Java8如何使用Lambda表达式进行比较
2023-12-09 19:27:16
详谈Java几种线程池类型介绍及使用方法
2023-10-13 03:27:30
![](https://img.aspxhome.com/file/2023/1/92671_0s.jpg)
Java日期与时间类原理解析
2021-07-20 14:00:36
Android登录记住多个密码的实现方法
2021-12-08 00:33:15
![](https://img.aspxhome.com/file/2023/9/139239_0s.png)
C#执行SQL事务用法实例
2021-11-23 23:32:27
![](https://img.aspxhome.com/file/2023/4/122724_0s.png)
Spring Cache和EhCache实现缓存管理方式
2023-01-17 01:44:40
![](https://img.aspxhome.com/file/2023/7/64927_0s.png)
spring boot整合redis主从sentinel方式
2021-10-01 20:13:47
![](https://img.aspxhome.com/file/2023/7/68227_0s.png)
C#中fixed关键字的作用总结
2023-07-17 09:43:44
Lombok基本注解之@SneakyThrows的作用
2021-12-24 15:30:47
![](https://img.aspxhome.com/file/2023/0/77340_0s.png)
C# using的本质及使用详解
2022-10-10 06:11:23
![](https://img.aspxhome.com/file/2023/0/76390_0s.png)
Java中的同步与异步详细介绍
2023-06-23 00:42:08
Android DrawLayout结合ListView用法实例
2021-10-29 02:30:28
![](https://img.aspxhome.com/file/2023/7/137837_0s.gif)
ListView实现下拉动态渲染数据
2022-10-31 11:19:12
![](https://img.aspxhome.com/file/2023/8/137918_0s.gif)
Linux中Java开发常用软件安装方法总结
2022-03-11 16:21:03
![](https://img.aspxhome.com/file/2023/2/85912_0s.png)
javaWeb项目部署到阿里云服务器步骤详解
2023-11-07 05:21:36
![](https://img.aspxhome.com/file/2023/9/58909_0s.png)
java实现简单石头剪刀布游戏
2023-07-20 05:31:37
![](https://img.aspxhome.com/file/2023/8/57628_0s.jpg)
C语言中的指针以及二级指针代码详解
2022-09-04 21:40:28
![](https://img.aspxhome.com/file/2023/4/117764_0s.png)
Android中使用 AutoCompleteTextView 实现手机号格式化附带清空历史的操作
2021-07-05 17:08:43
![](https://img.aspxhome.com/file/2023/7/87287_0s.png)
一篇文章带你玩转Spring bean的终极利器
2022-08-20 10:47:33
![](https://img.aspxhome.com/file/2023/1/62171_0s.jpg)