先前做了一个小视频的功能,里面有播放多个视频的功能,为了效率,我加了视频缓存功能;

一方面耗费用户的流量,另一方面直接从本地播放要更流畅

网上看资料,一个视频缓存库,使用起来很方便,还不错,就分享给大家

   //视频缓存
implementation 'com.danikula:videocache:2.7.1'

效果

代码:

public class MainActivity extends AppCompatActivity {

    private static final int MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE = 1;

    String url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
VideoView videoView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = findViewById(R.id.videoView); //检查版本是否大于M
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
} else {
Log.i("aaa", "权限已申请");
initVideo();
}
}
} private void initVideo() {
HttpProxyCacheServer proxy = VideoApplication.getProxy(this);
//1.我们会将原始url注册进去
// proxy.registerCacheListener(, bean.getVideo_url());
//2.我们播放视频的时候会调用以下代码生成proxyUrl
String proxyUrl = proxy.getProxyUrl(url);
if (proxy.isCached(url)) {
Log.i("aaaa", "已缓存");
} else {
Log.i("aaaa", "未缓存");
}
Log.i("aaaapath", proxyUrl);
videoView.setVideoPath(proxyUrl);
videoView.start();
videoView.findFocus();
} @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initVideo();
} else {
//"权限已拒绝";
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
} }

日志:

这样可以在本地的磁盘里找到视频了

Application代码:

public class VideoApplication extends Application {

    @Override
public void onCreate() {
super.onCreate();
} private HttpProxyCacheServer proxy; public static HttpProxyCacheServer getProxy(Context context) {
VideoApplication app = (VideoApplication) context.getApplicationContext();
return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
} private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.maxCacheSize(1024 * 1024 * 1024) // 1 Gb for cache
.fileNameGenerator(new MyFileNameGenerator())
.build();
}
}

记住不要忘记了AndroidManifest权限

  <uses-permission android:name="android.permission.INTERNET" />
<!--用于写入缓存数据到扩展存储卡-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />

代码下载:

https://github.com/DickyQie/android-video/tree/video-cache

参考文档(库地址)

https://github.com/danikula/AndroidVideoCache

https://blog.csdn.net/zhqw_csdn/article/details/81514313

最新文章

  1. django使用ldap认证
  2. 【转】C#多线程
  3. PHPCMS \phpcms\modules\member\index.php 用户登陆SQL注入漏洞分析
  4. Zabbix3.0 自动电话报障
  5. View (一)LayoutInflater()方法详解
  6. 学习笔记——Maven实战(四)基于Maven的持续集成实践
  7. php文件上传参数设置
  8. Mysql 排序优化与索引使用(转)
  9. WCF、WebAPI、WCF REST、Web Service之间的区别
  10. NFC标签
  11. 12.HTML编辑器(CKEditor、CKFinder集成)
  12. java.util.ConcurrentHashMap (JDK 1.8)
  13. Python爬虫【实战篇】百度翻译
  14. EF学习笔记(十二):EF高级应用场景
  15. Python-eval()函数
  16. 从零开始学 Web 之 ES6(四)ES6基础语法二
  17. 安装Adobe Acrobat XI Pro
  18. jquery 实现的全选demo
  19. 使用dshow抓取摄像头数据时,回调函数时间为0的问题
  20. json 的样式与应用 - C#/.NET

热门文章

  1. Kafka Streams开发入门(2)
  2. angular8 打包时 文件过大 导致内存溢出解决方案(记录)
  3. 接口自动化--unittest
  4. 【PyTorch v1.1.0文档研习】60分钟快速上手
  5. QTAction Editor的简单使用(简洁明了)
  6. 同时引入依赖:spring-cloud-starter-gateway 和 spring-boot-starter-web,报错
  7. Redis Cluster数据分片机制
  8. redux沉思录
  9. v熬夜是场可怕的自杀
  10. drf常用方法