android多媒体类VideoView使用方法详解
作者:liu_keke 发布时间:2023-12-12 03:37:47
标签:android,多媒体类,VideoView
一、概述
VideoView类将视频的显示和控制集于一身,我们可以借助它完成一个简易的视频播放器。VideoView和MediaPlayer也比较相似。
二、VideoView的使用方法
它主要有以下几种常用方法
步骤:
1.指定视频文件的路径,
2.接下来调用start()方法就可以开始播放视频,pause()方法就会暂停播放,resume()方法就会重新播放
注:获取视频文件也需要运行时权限,所有相关逻辑也需要写。
最后不要忘记在AndroidManifest.xml文件中声明用到的权限
下面是一个比较简单的播放、暂停、重新播放的小demo
一、xml文件中的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lk.playvideotest.MainActivity">
<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="Play"
android:textAllCaps="false"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause"
android:textAllCaps="false"/>
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RePlay"
android:textAllCaps="false"/>
</LinearLayout>
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
二、activity中的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.video_view);
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
Button replay = (Button) 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(), "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();
finish();
}
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();//将VideoView所占用的资源释放掉
}
}
}
三、声明权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
VideoView并不是一个万能的视频播放工具类,如果使用它,编写一个功能十分强大的视频播放器是不太现实的。VideoView适用于一些游戏的片头动画,或者某个应用的视频宣传。
来源:http://blog.csdn.net/rocoloco/article/details/78035702


猜你喜欢
- SpringBoot中的SpringMVC配置功能都是在WebMvcAutoConfiguration类中,xxxxAutoConfigur
- 接收到这样一个需求,就是英文名字中firstName和lastName,其中任何一个为null,就返回Empty。刚拿到需求,这不简单,if
- 前言P6Spy是一个框架,它可以无缝地拦截和记录数据库活动,而无需更改现有应用程序的代码。一般我们使用的比较多的是使用p6spy打印我们最后
- 首先分析一下问题:其实这个红框不是android的bug,把编译模式从eng改成user就可以了,红框只是eng模式debug的时候提示你系
- 前言RedisTemplate是Spring对于Redis的封装。如上图所示,RedisTemplate中定义了对5种数据结构操作。redi
- 这将会是一篇比较 * 的文章,当你想在某个人的生活中制造悲剧时你可能会去google搜索它。在Java的世界里,内存溢出仅仅只是你
- hadoop做的一个简单grep程序,可从文档中提取包含某些字符串的行/* * 一个简单grep程序,可从文档中提取包含莫些字符串
- wait(), notify(), notifyAll()等方法介绍在Object.java中,定义了wait(), notify()和no
- 本文介绍一个C#函数,可以实现计算文件的MD5值,可以用于文件传输后进行有效性校验。我们知道可以通过将一个字符串进行散列(Hash)运算得到
- Mybatis Log Plugin使用今天发现大部分猿友关于查看执行sql语句的方法,只知道将其输出到控制台。然而还有更简便的方法,就是使
- 原因每次使用idea新建项目,就会在默认的c盘下的一个maven仓库中下载jar包,可是我自己指定maven仓库不是这个。如何让idea在新
- 给大家看个计算题,看看大家的算术能力。0.1 +0.1 +0.1 - 0.3 等于几?大家可能会说这么简单的问题,是不是看不起我?肯定等于0
- springboot整合mybatis实现数据库更新批处理1.在mapper接口中编写方法/** * 修改book表中的销量和库存
- JVM内存模型在JVM中内存被分成两大块,分别是堆内存和堆外内存,堆内存就是JVM使用的内存,而堆外内存就是非JVM使用的内存,一般是分配给
- 突然对悬浮窗体感兴趣,查资料做了个小Demo,效果是点击按钮后,关闭当前Activity,显示悬浮窗口,窗口可以拖动,双击后消失。效果图如下
- 前言今天起床,拿起手机开机第一时间当然是打开微信了,左右滑动Viewpager,发现它使用了一种叫惰性加载,或者说懒加载(lazy-load
- ViewPager是android-support-v4.jar包里的组件。在布局文件里标签需要连包名一起写全称<android.su
- package com.cooly;import java.util.LinkedList;/*** @author coolyqq*模拟打
- 1.创建项目修改依赖版本2.创建配置文件package com.huanmingjie.elasticsearch.config;impor
- 今天碰到一个非常奇怪的问题: 在Android中ImageView无法显示加载的本地SDCard图片。 具体过程是:先调用本地照相机程序摄像