3

I am very confused with ExoPlayer and their documentation. Can please explain me What purpose and when we should use CacheDataSource and when SimpleCache?

nirazverma
  • 1,001
  • 10
  • 15

1 Answers1

3

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.

Sdghasemi
  • 5,370
  • 1
  • 34
  • 42
  • whats different between DataSource.Factory and DataSource? – nirazverma Sep 15 '20 at 09:57
  • `Factory` classes or interfaces as the name represents does the instance creation job for you in an easier way, you pass your data to the factory class and the class handles instance creation whenever needed. In this case you build a `CacheDataSourceFactory` instance and pass it up to construct a `MediaSource`. The `ProgressiveMediaSource.Factory` calls [`createDataSource()`](https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/upstream/DataSource.Factory.html#createDataSource--) method on your `DataSource.Factory` instance to create a `DataSource` instance. – Sdghasemi Sep 16 '20 at 03:59
  • @CoffeeJunkie As you can see in my second code block I have used a `mediaUri` to instantiate a `MediaSource` object. Use `Uri.parse(videoUrl)` to convert your url to a uri. – Sdghasemi Jan 06 '22 at 08:12
  • Oh ok but it doesn't work with the latest version, can you check my question and give me an alternative please? https://stackoverflow.com/questions/70595132/android-kotlin-how-to-use-exoplayer-simplecache-as-of-today @Sdghasemi –  Jan 06 '22 at 17:20
  • To can I cache list of videos? – Rakesh Saini Dec 22 '22 at 18:07