I am using ExoPlayer
to play videos from online server. So, for better to save more internet or data for replay video I'm just caching all videos to Cache directory
. But problem its saying that deprecated constructor
of SimpleCache
.
See my code:
private var sDownloadCache: SimpleCache? = null
fun getInstance(mContext: Context): SimpleCache {
val cacheEvictor = LeastRecentlyUsedCacheEvictor((100 * 1024 * 1024).toLong())
if (sDownloadCache == null)
sDownloadCache = SimpleCache(File(mContext.getCacheDir(), "media"),
cacheEvictor)
return sDownloadCache!!
}
In this code compiler is just warn me constructor SimpleCache(File!, CacheEvictor!) is deprecated. Deprecated in Java. So, after that I'm trying to find way in file of SimpleCache.java
there is a way to use SimpleCache
now is..
SimpleCache(File cacheDir, CacheEvictor evictor, DatabaseProvider databaseProvider)
And its new to me. So, my problem is how to use this new constructor instead of old deprecated one? Is there way to pass DatabaseProvider
? If yes then how or from what?
I'm using ExoPlayer
libraries:
implementation 'com.google.android.exoplayer:exoplayer:2.10.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.10.1'
And its because this version provides easy to create ExoPlayerFactory
instance
and build ProgressiveMediaSource
.
A Cache implementation that maintains an in-memory representation. Only one instance of SimpleCache is allowed for a given directory at a given time. To delete a SimpleCache, use delete(File, DatabaseProvider) rather than deleting the directory and its contents directly.
Try this:
val evictor = LeastRecentlyUsedCacheEvictor((100 * 1024 * 1024).toLong())
val databaseProvider: DatabaseProvider = ExoDatabaseProvider(mContext)
simpleCache = SimpleCache(File(mContext.cacheDir, "media"), evictor, databaseProvider)
val mediaSource = ProgressiveMediaSource.Factory(
simpleCache?.let {
CacheDataSourceFactory(
mContext,
100 * 1024 * 1024, 10 * 1024 * 1024, playUri, it
)
}
)
.createMediaSource(playUri)
exoPlayer?.prepare(mediaSource)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With