Android谷歌Exoplayer 2.2 HLS和DASH流缓存

时间:2017-03-16 09:25:10

标签: android caching streaming okhttp3 exoplayer

我正在尝试缓存HLS和DASH流视频, 我尝试了很多解决方案,但没有使用Exoplayer v2.2 许多问题重定向到以下链接,但没有得到任何适当的解决方案。 https://github.com/google/ExoPlayer/issues/420Using cache in ExoPlayer

在一个解决方案中,在Google Exoplayer 2.2中找不到“ExtractorSampleSource”类

OkHttpClient okHttpClient = new OkHttpClient.Builder().cache(new okhttp3.Cache(context.getCacheDir(), 1024000)).build();
OkHttpDataSource okHttpDataSource = new OkHttpDataSource(okHttpClient, "android", null);
OkHttpDataSource ok2 = new OkHttpDataSource(okHttpClient, "android", null);
HttpDataSource dataSource = new CacheDataSource(context, okHttpDataSource, ok2);
ExtractorSampleSource sampleSource = new ExtractorSampleSource(
                uri,
                dataSource,
                allocator,
                buffer_segment_count * buffer_segment_size,
                new Mp4Extractor(), new Mp3Extractor());

在其他解决方案中,在v2.2中找不到相同的错误'DefaultUriDataSource'类

DataSource dataSource = new DefaultUriDataSource(context, null, new OkHttpDataSource(getClient(context), userAgent, null, null/*, CacheControl.FORCE_CACHE*/));

所有解决方案都需要1到2年的时间,并且不支持最新版本的Google Exoplayer v2.2。

任何人都有想法或任何样本或任何解决方案来使用HLS和DASH流进行缓存?

1 个答案:

答案 0 :(得分:2)

在buildDataSourceFactory下面使用并存储缓存

    DataSource.Factory buildDataSourceFactory(boolean cache) {

       if (!cache) {
        return new DefaultDataSourceFactory(context, BANDWIDTH_METER,
                buildHttpDataSourceFactory(BANDWIDTH_METER));
       }else{

       return new DataSource.Factory() {
           @Override
           public DataSource createDataSource() {
               LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(100 * 1024 * 1024);
               SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media_cache"), evictor);


               return new CacheDataSource(simpleCache, buildCachedHttpDataSourceFactory(BANDWIDTH_METER).createDataSource(),
                       new FileDataSource(), new CacheDataSink(simpleCache, 10 * 1024 * 1024),
                       CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null);
           }

       };
   }
    }

    private DefaultDataSource.Factory buildCachedHttpDataSourceFactory(DefaultBandwidthMeter bandwidthMeter) {
        return new DefaultDataSourceFactory(context, bandwidthMeter, buildHttpDataSourceFactory(bandwidthMeter));
    }