How to implement caching in ExoPlayer?

How to implement caching in ExoPlayer? It’s very bad that he always overloads all the videos ...

In my RendererBuilder, I used the following approach:

Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);

DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(player.getMainHandler(),
    null);

SimpleCache cache = (SimpleCache) Singleton.getSingleton(context).getCache();

// Always prints 0, that the problem!
Log.d("CacheSize", String.valueOf(cache.getCacheSpace()));

DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);

CacheDataSource cacheDataSource = new CacheDataSource(cache, dataSource, false, false);


ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri, cacheDataSource, allocator,
    BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE);

And inside my Singleton constructor:

this.cache = new SimpleCache(context.getApplicationContext().getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 8));

But it does not seem to work, since the video takes some time, and the loaded cache space is always 0! I tried to make some searches on this question, but did not answer every question about it, I never saw a working example.

+4
source share

Source: https://habr.com/ru/post/1615412/


All Articles