导入到项目中:
compile 'com.danikula:videocache:2.6.4'
在Application配置:
public class MyApp extends MultiDexApplication {
private HttpProxyCacheServer proxy;
public static HttpProxyCacheServer getProxy(Context context) {
MyApp app = (MyApp) context.getApplicationContext();
return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
}
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer(this);
}
}
在代码中使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HttpProxyCacheServer proxy = getProxy();
String proxyUrl = proxy.getProxyUrl(VIDEO_URL);//传入视频的url
videoView.setVideoPath(proxyUrl); //给webview设置proxyUrl
}
private HttpProxyCacheServer getProxy() {
return MyApp.getProxy(getApplicationContext());
}
API:
缓存限制:
文件大小限制:
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.maxCacheSize(1024 * 1024 * 1024) // 设置缓存最大限制
.build();
}
文件总数限制:
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.maxCacheFilesCount(20) //设置最多缓存多少个文件
.build();
}
监听缓存进度:
proxy.registerCacheListener(this, url);
@Override
public void onCacheAvailable(File file, String url, int percentsAvailable) {
progressBar.setSecondaryProgress(percentsAvailable); //设置进度条
setCachedState(percentsAvailable == 100);
}