温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android VideoCache视频缓存的方法详解

发布时间:2020-10-08 10:04:12 来源:脚本之家 阅读:650 作者:程序小渣渣 栏目:移动开发

Android VideoCache视频缓存的方法详解

项目中遇到视频播放,需要加载网络url,不可能每次都进行网络加载,当然了,就需要用到我们的缓存机制

AndroidVideoCache

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());
}

最后视频加载的 时候需要判断是否缓存,做一些比如缓冲进度条的隐藏等操作

以上就是Android VideoCache的使用方法详解,本站关于Android开发的文章还有很多,希望大家搜索查阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI