Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear cache of exoplayer

I have referred this link for creating CachedDataSource for Exo-Player and successfully created it, but I am not sure, how to delete the cache of the loaded video?
There is a code example here , but I am not sure how to implement it.Any help will be appreciated.

like image 260
Shashank Mishra Avatar asked Nov 02 '25 13:11

Shashank Mishra


1 Answers

if you created a cache folder like it described in https://exoplayer.dev/downloading-media.html example for kotlin

@Synchronized
fun getDownloadCache(): Cache {
    if (downloadCache == null) {
        val downloadContentDirectory = File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY)
        downloadCache = SimpleCache(downloadContentDirectory, NoOpCacheEvictor(), getDatabaseProvider())
    }
    return downloadCache as Cache
}

you can simply access this cache anywhere in your code to do this

fun clearCache() {
    application.getDownloadCache().release()
}

Or clean single key with this:

fun removeKeyFromCache(key: String) {
    CacheUtil.remove(application.getDownloadCache(), key)
}

application is an instance of your app, which can be created like this

lateinit var singleton: MyApp
open class MyApp: Application() {
  override fun onCreate() {
    super.onCreate()
    singleton = this
  }
}

And access it from anywhere:

private val application: MyApp = singleton
like image 136
javier Cuervas Avatar answered Nov 04 '25 03:11

javier Cuervas