android -------- VideoCache 视频播放(缓存视频到本地)
2024-08-24 12:08:09
先前做了一个小视频的功能,里面有播放多个视频的功能,为了效率,我加了视频缓存功能;
一方面耗费用户的流量,另一方面直接从本地播放要更流畅
网上看资料,一个视频缓存库,使用起来很方便,还不错,就分享给大家
//视频缓存
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
最新文章
- django使用ldap认证
- 【转】C#多线程
- PHPCMS \phpcms\modules\member\index.php 用户登陆SQL注入漏洞分析
- Zabbix3.0 自动电话报障
- View (一)LayoutInflater()方法详解
- 学习笔记——Maven实战(四)基于Maven的持续集成实践
- php文件上传参数设置
- Mysql 排序优化与索引使用(转)
- WCF、WebAPI、WCF REST、Web Service之间的区别
- NFC标签
- 12.HTML编辑器(CKEditor、CKFinder集成)
- java.util.ConcurrentHashMap (JDK 1.8)
- Python爬虫【实战篇】百度翻译
- EF学习笔记(十二):EF高级应用场景
- Python-eval()函数
- 从零开始学 Web 之 ES6(四)ES6基础语法二
- 安装Adobe Acrobat XI Pro
- jquery 实现的全选demo
- 使用dshow抓取摄像头数据时,回调函数时间为0的问题
- json 的样式与应用 - C#/.NET