androidx / androidx/media

Sharing SimpleCache in multiple running ExoPlayers

Open
#434 0 comments 0 reactions 1 assignee View on GitHub

@marcbaechinger is already working on this.

Since Jun 2, 2023.

needs triage question
Dominant language
Java
Stars
3k
Forks
955
Avg merge
12d 14h
Merged PRs (30d)
2

Description

I have two instances of ExoPlayer running and playing audio at the same time. I would like to be able to use a shared cache because they will both play from the same pool of content.

My idea is to create a single SimpleCache instance and then create two ExoPlayer instances, with separate CacheDataSource.Factory using the shared Cache instance.

This is a simplified version of the code I have in mind:

val cacheDir = context.cacheDir.absoluteFile.resolve("exoplayer")
val evictor = LeastRecentlyUsedCacheEvictor(100_000_000)
val databaseProvider = StandaloneDatabaseProvider(context)
val cache = SimpleCache(cacheDir, evictor, databaseProvider)
val playerA = createExoPlayer(context, cache)
val playerB = createExoPlayer(context, cache)


private fun createExoPlayer(context: Context, cache: Cache): ExoPlayer {
    val httpDataSourceFactory = DefaultHttpDataSource.Factory()
    val defaultDataSourceFactory = DefaultDataSource.Factory(context, httpDataSourceFactory)
    val cacheDataSourceFactory = CacheDataSource.Factory()
        .setUpstreamDataSourceFactory(defaultDataSourceFactory)
        .setCache(cache)
        .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
    val mediaSourceFactory = ProgressiveMediaSource.Factory(cacheDataSourceFactory)
    return ExoPlayer.Builder(context)
        .setMediaSourceFactory(mediaSourceFactory)
        .build()
}

Does SimpleCache support this? Is access to the database and filesystem synchronized? I see a lot of synchronization in the SimpleCache sources, but no explicit documentation saying it's safe to do this.

If not, is there another way?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.