Using cache in ExoPlayer

后端 未结 10 2190
暖寄归人
暖寄归人 2020-11-29 18:29

I\'m looking for any example of implementing cache in ExoPlayer.

ExoPlayer has in its library different classes concerning cache and Google explain in this video th

10条回答
  •  Happy的楠姐
    2020-11-29 18:40

    To resolve the problem of multiple videos or processes trying to access the same cache, you need a true Singleton. A reliable way would be to do it this way:

    object VideoCache {
        private var sDownloadCache: SimpleCache? = null
        private const val maxCacheSize: Long = 100 * 1024 * 1024
    
        fun getInstance(context: Context): SimpleCache {
            val evictor = LeastRecentlyUsedCacheEvictor(maxCacheSize)
            if (sDownloadCache == null) sDownloadCache = SimpleCache(File(context.cacheDir, "koko-media"), evictor)
            return sDownloadCache as SimpleCache
        }
    }
    

    which you can now use:

    private val simpleCache: SimpleCache by lazy {
            VideoCache.getInstance(context)
        }
    

提交回复
热议问题