如何在Android中后台缓存视频?

Hit*_*ani 2 video android caching android-video-player

我正在构建一个Android应用程序,用户可以在其中查看一些列出的视频。这些视频是某个频道中的类别。用户选择了某个频道后,我想将与该频道相关的所有视频缓存到我的缓存中,这样就可以在没有互联网的情况下播放视频。

任何人都可以在不玩游戏的情况下对视频缓存有更多的了解,请帮助我了解如何实现此任务。

现在,如果使用某些库播放的视频,我可以将其缓存。

Sag*_*gar 5

我找到了以下使用lib以下在后台(单个/多个)中缓存视频的工作解决方案,不需要player/video_view.useAsyncTaskRunner

视频缓存库

在gradle文件中添加以下内容

compile 'com.danikula:videocache:2.7.0'
Run Code Online (Sandbox Code Playgroud)

由于我们只需要启动预取,因此无需while循环执行任何操作。

或者我们可以使用ByteArrayOutputStream将数据写到磁盘上。

 URL url = null;
 try {
       url = new URL(cachingUrl(cachingUrl));

       InputStream inputStream = url.openStream();
       int bufferSize = 1024;
       byte[] buffer = new byte[bufferSize];
       int length = 0;
       while ((length = inputStream.read(buffer)) != -1) {
             //nothing to do
       }
    } catch (IOException e) {
      e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

来自lib的重要代码。去做

使用以下代码在应用程序类中创建静态实例

private HttpProxyCacheServer proxy;

public static HttpProxyCacheServer getProxy(Context context) {
    Applications app = (Applications) context.getApplicationContext();
    return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
}

private HttpProxyCacheServer newProxy() {
    //return new HttpProxyCacheServer(this);
    return new HttpProxyCacheServer.Builder(this)
            .cacheDirectory(CacheUtils.getVideoCacheDir(this))
            .maxCacheFilesCount(40)
            .maxCacheSize(1024 * 1024 * 1024)
            .build();
}
Run Code Online (Sandbox Code Playgroud)

在您的活动中编写以下代码以通过 url

public String cachingUrl(String urlPath) {

 return Applications.getProxy(this).getProxyUrl(urlPath, true);

}
Run Code Online (Sandbox Code Playgroud)

  • 该库显示了 2018 年 7 月 24 日的最新提交...是否有大玩家或像 Glide 那样积极维护的图像 (2认同)