CacheDataSource vs SimpleCache in exoplayer?
CacheDataSource
and SimpleCache
fulfill two different purposes. If you take a look at their class prototype you will see that CacheDataSource implements DataSource
and SimpleCache implements Cache
. When you need to cache your downloaded videos you have to use CacheDataSource
as your DataSource.Factory
to prepare your media playback:
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "AppName"));
dataSourceFactory = new CacheDataSourceFactory(VideoCacheSingleton.getInstance(), dataSourceFactory);
And then use dataSourceFactory
to create a MediaSource
:
// This is the MediaSource representing the media to be played.
MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(mediaUri);
SimpleExoPlayer exoPlayerInstance = new SimpleExoPlayer.Builder(context).build();
exoPlayerInstance.prepare(mediaSource);
While SimpleCache
offers you a cache implementation that maintains an in-memory representation. As you can see in the first code block a CacheDataSourceFactory constructor needs a Cache
instance to work with. You can either declare your own caching mechanism or use the default SimpleCache
class that ExoPlayer provides you. If you need to use the default implementation you should keep this in mind:
Only one instance of SimpleCache is allowed for a given directory at a given time
As per the documentation. So in order to use a single instance of SimpleCache
for a folder we use singleton declaration pattern:
public class VideoCacheSingleton {
private static final int MAX_VIDEO_CACHE_SIZE_IN_BYTES = 200 * 1024 * 1024; // 200MB
private static Cache sInstance;
public static Cache getInstance(Context context) {
if (sInstance != null) return sInstance;
else return sInstance = new SimpleCache(new File(context.getCacheDir(), "video"), new LeastRecentlyUsedCacheEvictor(MAX_VIDEO_CACHE_SIZE_IN_BYTES), new ExoDatabaseProvider(context)));
}
}
TL; DR
We use CacheDataSource
to prepare a caching media playback and SimpleCache
to build its DataSource.Factory
instance.
ExoPlayer how to save cache?
There is a relatively simple approach (however a bit naive) that works for most simple test cases, you can then proceed to extend it to suit your needs and maybe even improve and contribute to the official ExoPlayer project.
I've implemented it like this in the renderer builder
Cache cache = new SimpleCache(context.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 10));
DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
CacheDataSource cacheDataSource = new CacheDataSource(cache, dataSource, false, false);
HlsChunkSource chunkSource = new HlsChunkSource(cacheDataSource, url, manifest, bandwidthMeter,
variantIndices, HlsChunkSource.ADAPTIVE_MODE_SPLICE, audioCapabilities);
Related Topics
How to Hide Toolbar When I Scrolling Content Up
What Is a Maximum Size of SQLite Database on Android
How to Tell If the Soft-Keyboard Is Shown
How to Disable Status Bar Click and Pull Down in Android
Using Camera in the Android Emulator
Run Task Before Compilation Using Android Gradle Plugin
How to Open My Existing Eclipse Projects in Android Studio
Sqlite Exception No Such Column When Trying to Select
Pagination Not Work for the Recyclerview Within Nestedscrollview
Continuous Speech Recognition Android
How to Set Alarm in Android Programmatically
Suppress/Block Broadcastreceiver in Another App
Changing Default Port (I.E. 5037) on Which Adb Server Runs
Set Actionbar Tabs at Bottom in Actionbaractivity
Getactionbar() Returns Null (Appcompat-V7 21)
Why "This App Has Been Built with an Incorrect Configuration" Error Occured in Some Phones
How to Reference an Asset in a Library Project
Can the Android Emulator Record and Play Back Audio Using Pc Hardware