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 步:在应用的 application 类中创建缓存策略
public class myapplication extends application {
public simplecache simplecache;
public void oncreate() {
super.oncreate();
//缓存最大值为100m
leastrecentlyusedcacheevictor 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);
发表评论