Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 540
Nick Bapu Avatar asked Jul 23 '19 07:07

Nick Bapu


People also ask

How do I clear my ExoPlayer cache?

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.


1 Answers

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)
like image 200
Naveen Dew Avatar answered Oct 04 '22 10:10

Naveen Dew