android -------- VideoCache 视频播放(缓存视频到本地)
先前做了一个小视频的功能,里面有播放多个视频的功能,为了效率,我加了视频缓存功能;
一方面耗费用户的流量,另一方面直接从本地播放要更流畅
网上看资料,一个视频缓存库,使用起来很方便,还不错,就分享给大家
//视频缓存 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
分类:
Android
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
2017-10-24 Java开源-astar:A 星算法