Created
May 17, 2019 06:19
-
-
Save realpacific/ef6d3876f2f3ec8fdc3b3fedda0f546d to your computer and use it in GitHub Desktop.
Implementing cache in ExoPlayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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