Sharing SimpleCache in multiple running ExoPlayers
@marcbaechinger is already working on this.
Since Jun 2, 2023.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.