2017-03-16 116 views
2

我試圖緩存HLS和DASH流視頻, 我已經嘗試了很多解決方案,但沒有與Exoplayer v2.2工作 許多問題重定向到下面的鏈接,但沒有得到任何適當的解。 https://github.com/google/ExoPlayer/issues/420Using cache in ExoPlayerAndroid谷歌Exoplayer 2.2 HLS和DASH流緩存

在一個解決方案 'ExtractorSampleSource' 類沒有在谷歌Exoplayer發現2.2

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

在其他解決方案得到了同樣的錯誤 'DefaultUriDataSource' 類v2.2中沒有發現

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

使用下面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)); 
    }