Android视频缓存框架,避免多次或重复下载消耗更多的时间和流量。
- 流式传输期间缓存到磁盘
- 离线使用缓存资源
- 部分加载
- 缓存限制(最大缓存大小,最大文件数)
- 多个客户端访问同一缓存
使用代理URL替代原始URL来开启URL,Android P及以上请关闭禁止明文通信
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HttpProxyCacheServer proxy = getProxy();
String proxyUrl = proxy.getProxyUrl(VIDEO_URL);
videoView.setVideoPath(proxyUrl);
}
private HttpProxyCacheServer getProxy() {
// should return single instance of HttpProxyCacheServer shared for whole app.
}
为了保证正常工作,您应该为整个应用程序使用单个实例HttpProxyCacheServer。例如,您可以将共享代理存储在您的Application:
public class App extends Application {
private HttpProxyCacheServer proxy;
public static HttpProxyCacheServer getProxy(Context context) {
App app = (App) context.getApplicationContext();
return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
}
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer(this);
}
}
默认情况下HttpProxyCacheServer使用 512Mb 缓存文件。您可以更改此值:
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.maxCacheSize(1024 * 1024 * 1024) // 1 Gb for cache
.build();
}
或者可以限制缓存中的文件总数:
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.maxCacheFilesCount(20)
.build();
}
甚至实施您自己的DiskUsage策略:
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.diskUsage(new MyCoolDiskUsageStrategy())
.build();
}
默认AndroidVideoCache使用视频 url 的 MD5 作为文件名。但在某些情况下 url 不稳定,它可能包含一些生成的部分(例如会话令牌)。在这种情况下,缓存机制将被破坏。要修复它,您必须提供自己的FileNameGenerator:
public class MyFileNameGenerator implements FileNameGenerator {
// Urls contain mutable parts (parameter 'sessionToken') and stable video's id (parameter 'videoId').
// e. g. http://example.com?videoId=abcqaz&sessionToken=xyz987
public String generate(String url) {
Uri uri = Uri.parse(url);
String videoId = uri.getQueryParameter("videoId");
return videoId + ".mp4";
}
}
HttpProxyCacheServer proxy = HttpProxyCacheServer.Builder(context)
.fileNameGenerator(new MyFileNameGenerator())
.build()
您可以在以下帮助下向请求添加自定义标头HeadersInjector:
public class UserAgentHeadersInjector implements HeaderInjector {
@Override
public Map<String, String> addHeaders(String url) {
return Maps.newHashMap("User-Agent", "Cool app v1.1");
}
//header key是否有效
@Override
public Boolean filter(String url, String key) {
return null;
}
}
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.headerInjector(new UserAgentHeadersInjector())
.build();
}
自定义视频下载
public class MySource implements Source {
@Override
public void open(long offset) throws ProxyCacheException {
}
@Override
public long length() throws ProxyCacheException {
return 0;
}
@Override
public int read(byte[] buffer) throws ProxyCacheException {
return 0;
}
@Override
public void close() throws ProxyCacheException {
}
@Override
public String getMime() throws ProxyCacheException {
return null;
}
@Override
public String getUrl() {
return null;
}
}
public class MySourceCreator implements SourceCreator<MySource> {
@Override
public MySource create(String url, SourceInfoStorage sourceInfoStorage, Map<String, String> headers) {
return null;
}
@Override
public MySource create(MySource source) {
return null;
}
}
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.sourceCreator(new MySourceCreator())
.build();
}
将本仓库引入你的项目:
合并以下代码到项目根目录下的build.gradle文件的repositories尾。
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
合并以下代码到需要使用的application Module的dependencies尾。
dependencies {
...
compile 'com.iwdael:videocache:$version'
}
Power by danikula/AndroidVideoCache