使用Exoplayer 2.2的VOD缓存

时间:2017-03-16 06:43:48

标签: android caching okhttp3 exoplayer

在使用exoplayer对视频/音频内容进行缓存之后,我觉得Jacek Using cache in ExoPlayer给出的答案是实现无限范围请求缓存的正确方法,而不是使用内置的Exocache支持由https://github.com/google/ExoPlayer/issues/420& https://github.com/google/ExoPlayer/issues/57。 但他没有说过配置OkHttp& amp;缓存请求。

任何人都可以提供一些关于如何配置okhttp&的链接。缓存请求。

1 个答案:

答案 0 :(得分:0)

通过使buildDataSourceFactory配置更改如下,我们可以缓存视频内容

  private DataSource.Factory buildDataSourceFactory(final boolean useBandwidthMeter) {
//    return new DefaultDataSourceFactory(this, useBandwidthMeter ? BANDWIDTH_METER : null,
//        buildHttpDataSourceFactory(useBandwidthMeter));
    return new DataSource.Factory() {
      @Override
      public DataSource createDataSource() {
        LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(100 * 1024 * 1024);
        SimpleCache simpleCache = new SimpleCache(new File(getCacheDir(), "media_cache"), evictor);
        return new CacheDataSource(simpleCache, buildMyDataSourceFactory(useBandwidthMeter).createDataSource(),
                CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_CACHE_UNBOUNDED_REQUESTS,
                10 * 1024 * 1024);
      }
    };
  }

  private DefaultDataSource.Factory buildMyDataSourceFactory(boolean useBandwidthMeter) {
    return new DefaultDataSourceFactory(PlayerActivity.this, userAgent, useBandwidthMeter ? BANDWIDTH_METER : null);
  }