当前位置: 首页 > news >正文

Android Media3 ExoPlayer 开启缓存功能

    ExoPlayer 开启播放缓存功能,在下次加载已经播放过的网络资源的时候,可以直接从本地缓存加载,实现为用户节省流量和提升加载效率的作用。

方法一:采用 ExoPlayer 缓存策略

第 1 步:实现 Exoplayer

参考 Exoplayer 官网 Release notes :

对应关系:

2.19.0 (2023-07-05)  -- AndroidX Media3 1.1.0 release.

2.19.1 (2023-08-14)  -- AndroidX Media3 1.1.1 release

    Exoplayer 从 2.19.0 开始迁移至 AndroidX 的 Media3 框架内,2.19.1 是 Exoplayer 作为独立项目发布的最后一个版本,所以引入 Exoplayer 2.19.1 有以下两个方式,建议采用最新的方式 2。

# 方式1
implementation 'com.google.android.exoplayer:exoplayer-core:2.19.1'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.19.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.19.1'# 方式2
implementation "androidx.media3:media3-exoplayer:1.1.1"
implementation "androidx.media3:media3-exoplayer-dash:1.1.1"
implementation "androidx.media3:media3-ui:1.1.1"

第 2 步:在应用程序类中创建缓存策略

public SimpleCache simpleCache;
@Overridepublic void onCreate() {super.onCreate();//缓存最大值为100MLeastRecentlyUsedCacheEvictor leastRecentlyUsedCacheEvictor = new LeastRecentlyUsedCacheEvictor(100 * 1024 * 1024);if (simpleCache == null) {simpleCache = new SimpleCache(getCacheDir(), leastRecentlyUsedCacheEvictor, new ExoDatabaseProvider(this));}
}

第 3 步:加载数据源,实现缓存

//本地资源(如:/sdcard/media/1.mp4)或 HTTP 资源
Uri videoUri = Uri.parse("YOUR URL");
MediaItem mediaItem = MediaItem.fromUri(videoUri);
DefaultHttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(true);
// 这里的DefaultDataSource同时支持本地和HTTP请求的资源,自动实现检测 (The DefaultDataSource supports both local and Http sources. It automatically detects which one to use.)
DefaultDataSource.Factory defaultDataSourceFactory = new DefaultDataSourceFactory(requireContext(), httpDataSourceFactory);
//实现缓存
CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory().setCache(MyApplication.getAppInstance().simpleCache).setUpstreamDataSourceFactory(defaultDataSourceFactory).setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);MediaSource mediaSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory).createMediaSource(mediaItem);
player.setMediaSource(mediaSource, true);

方法二:  通过 Android Video Cache Library

    开源库 AndroidVideoCache 的原理是通过代理的策略实现一个中间层,将网络视频请求转移到本地实现的代理服务器上,这样真正请求的数据就会被代理拿到,然后代理一边向本地写入数据,一边根据需要的数据看是读网络数据还是读本地缓存数据,从而实现数据的复用。

第 1 步:实现 VideoCache

implementation 'com.danikula:videocache:2.7.1'

第 2 步:在应用程序类中存储共享代理

public class MyApplication extends Application {private HttpProxyCacheServer proxy;public static HttpProxyCacheServer getProxy(Context context) {MyApplication app = (MyApplication) context.getApplicationContext();return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;}private HttpProxyCacheServer newProxy() {return new HttpProxyCacheServer.Builder(this).maxCacheSize(1024 * 1024 * 1024).build();}}

第 3 步:Exoplayer 接入缓存

HttpProxyCacheServer proxy = getProxy(activity);
//注意应采用来自代理的 url 而不是原始 url 来添加缓存
String proxyUrl = proxy.getProxyUrl(VIDEO_URL);
PlayerView playerView = findViewById(R.id.video_view);
ExoPlayer player = ExoPlayerFactory.newSimpleInstance(VideoActivity.this,new DefaultRenderersFactory(this),new DefaultTrackSelector());
MediaSource mediaSource = buildMediaSource(proxyUrl);
player.prepare(mediaSource, true, false);
playerView.setPlayer(player);
http://www.lryc.cn/news/171485.html

相关文章:

  • MyBatis注解开发
  • C# Onnx Yolov8 Cls 分类
  • Fiddler常用的快键键
  • 【Linux】生产消费模型 + 线程池
  • 基于springboot+vue的爱心助农网站(前后端分离)
  • “华为杯”研究生数学建模竞赛2019年-【华为杯】D题:汽车行驶工况构建(附获奖论文和MATLAB代码实现)
  • v-cloak的作用和原理
  • pip pip3安装库时都指向python2的库
  • 和逸云 RK3229 如何进入maskrom强刷模式
  • 防静电离子风扇的应用及优点
  • git中无法使用方向键的问题
  • 负载均衡中间件---Nginx
  • Linux硬链接、软链接
  • React面试题总结(一)
  • 一句话设计模式12:适配器模式
  • iOS加固保护技术:保护你的iOS应用免受恶意篡改
  • 阿里云产品试用系列-云桌面电脑
  • vue3使用vue-virtual-scroller虚拟滚动遇到的问题
  • c#用Gnuplot画图源码
  • 【前端设计模式】之工厂模式
  • Hive 的函数介绍
  • 【Linux基础】第31讲 Linux用户和用户组权限控制命令(三)
  • html form表单高级用法
  • openssl升级
  • 【数据结构】图的遍历:广度优先(BFS),深度优先(DFS)
  • Mysql 学习总结(89)—— Mysql 库表容量统计
  • virtualBox安装配置使用
  • 北斗导航 | RTD、RTK完好性之B值、VPL与HPL计算(附B值计算matlab源代码)
  • more often than not 的含义
  • 【Linux】Linux环境配置安装