Skip to content

Instantly share code, notes, and snippets.

@realpacific
Created May 17, 2019 06:19
Show Gist options
  • Save realpacific/ef6d3876f2f3ec8fdc3b3fedda0f546d to your computer and use it in GitHub Desktop.
Save realpacific/ef6d3876f2f3ec8fdc3b3fedda0f546d to your computer and use it in GitHub Desktop.
Implementing cache in ExoPlayer
class LocalCacheDataSourceFactory(private val context: Context) : DataSource.Factory {
private val defaultDataSourceFactory: DefaultDataSourceFactory
//TODO Needs to be a singleton
private val simpleCache: SimpleCache = SimpleCache(
File(context.cacheDir, "media"),
LeastRecentlyUsedCacheEvictor(MAX_CACHE_SIZE)
)
private val cacheDataSink: CacheDataSink = CacheDataSink(simpleCache, MAX_FILE_SIZE)
private val fileDataSource: FileDataSource = FileDataSource()
init {
val userAgent = "Demo"
val bandwidthMeter = DefaultBandwidthMeter()
defaultDataSourceFactory = DefaultDataSourceFactory(
this.context,
bandwidthMeter,
DefaultHttpDataSourceFactory(userAgent)
)
}
override fun createDataSource(): DataSource {
return CacheDataSource(
simpleCache, defaultDataSourceFactory.createDataSource(),
fileDataSource, cacheDataSink,
CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment