Search code examples
androidkotlinexoplayerexoplayer2.x

Exo-Player 2.10.1 says SimpleCache(File cacheDir, CacheEvictor evictor) deprecated


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.


Solution

  • Try this:

            val evictor = LeastRecentlyUsedCacheEvictor((100 * 1024 * 1024).toLong())
            val databaseProvider: DatabaseProvider = StandaloneDatabaseProvider(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)