Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Exoplayer 2.2 HLS and DASH streaming cache

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?

like image 792
comeback4you Avatar asked Nov 29 '25 16:11

comeback4you


1 Answers

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));
    }
like image 90
comeback4you Avatar answered Dec 02 '25 07:12

comeback4you