Android VideoCache Android VideoCache视频缓存的方法详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android VideoCache Android VideoCache视频缓存的方法详解

程序小渣渣   2021-03-24 我要评论
想了解Android VideoCache视频缓存的方法详解的相关内容吗,程序小渣渣在本文为您仔细讲解Android VideoCache的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,VideoCache,Android,VideoCache如何使用,Android,VideoCache实例详解,下面大家一起来学习吧。

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开发的文章还有很多,希望大家搜索查阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们