3

I'm trying to caching HLS and DASH streaming video, I have tried many solution but not working with Exoplayer v2.2 many issue redirect to below links but not getting any proper solution. https://github.com/google/ExoPlayer/issues/420 and Using cache in ExoPlayer.

In the one solution 'ExtractorSampleSource' class is not found in Google 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());

In other solution got same error 'DefaultUriDataSource' class not found in v2.2

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

all the solutions are 1 to 2 year older and it's not supported latest version of Google Exoplayer v2.2.

any one have idea or any sample or any solution to do caching with HLS and DASH stream?

Community
  • 1
  • 1
comeback4you
  • 716
  • 1
  • 8
  • 24

1 Answers1

2

Used below buildDataSourceFactory and its storing the cache

    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));
    }
comeback4you
  • 716
  • 1
  • 8
  • 24
  • what is buildHttpDataSourceFactory method? – David Jan 01 '19 at 10:17
  • @David, DefaultDataSourceFactory(Context context, DataSource.Factory baseDataSourceFactory) In second parameter of this constructor you pass DataSource.Factory Which helps you to create DataSource for your Exoplayer media. – Sumit Jangir Jul 23 '20 at 13:36