AndroidVideoCache是一个视频/音频缓存库,利用本地代理实现了边下边播,使用起来非常简单。
HttpProxyCacheServer是主要类,是一个代理服务器,可以配置缓存文件的数量、缓存文件的大小、缓存文件的目录和缓存文件命名算法,文件缓存均基于LRU算法,利用Builder来配置:
//配置缓存目录 public Builder cacheDirectory(File file); //配置缓存文件命名规则 public Builder fileNameGenerator(FileNameGenerator fileNameGenerator) ; //配置缓存文件大小 public Builder maxCacheSize(long maxSize) ; //配置缓存文件数量 public Builder maxCacheFilesCount(int count) ;
建议以单列模式将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); } }
调用十分方便,只需要增加一行代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); HttpProxyCacheServer proxy = getProxy(); String proxyUrl = proxy.getProxyUrl(VIDEO_URL); videoView.setVideoPath(proxyUrl); } private HttpProxyCacheServer getProxy() { return App.getProxy(getApplicationContext()); }