启用 Exoplayer 缓存后滑动缓慢加载

问题描述

第一次在 stackoverflow 上写这篇文章。 (你打赌我是 Android 开发的菜鸟)

我一直在试验一个准 Spotify 克隆应用程序,它有一个 Recyclerview 显示歌曲缩略图并通过 URL 从 Firestore 数据库加载 mp3。该应用使用 Glide 显示图像并使用 ExoPlayer 播放 mp3。

除了在我启用 ExoPlayer 使用缓存播放后图像(目前大约 12 个)的加载速度变慢外,一切正常。在为 ExoPlayer 实现 Cache 之前,Glide 在启动时立即显示图像(不到 1 秒),但在为 ExoPlayer 使用 Cache 后,显示 6~7 行 Recyclerview 大约需要 3~4 秒。

ExoPlayer 在使用 cacheDataSoruce 之前准备

      private fun prepPlayerNormal(url: String?) { // NORMAL

            val uri = Uri.parse(url)
            val localMp3Uri = RawResourceDataSource.buildRawResourceUri(R.raw.rocounty_demo)
            val mediaItem = MediaItem.fromUri(uri)
            val mediaSource = ProgressiveMediaSource.Factory(DefaultDataSourceFactory(receivedContext,dataSourceFactory),DefaultExtractorsFactory()).createMediaSource(mediaItem)
            exoPlayer.setMediaSource(mediaSource)
            exoPlayer.prepare()
            exoPlayer.playWhenReady = true
        }

ExoPlayer 在使用 cacheDataSoruce 后准备

private fun prepPlayerWithCache(url:String?) { 

        val mp3Uri = Uri.parse(url)
        val mediaItem = MediaItem.fromUri(mp3Uri)
        val mediaSource = ProgressiveMediaSource.Factory(cacheDataSourceFactory).createMediaSource(mediaItem)

        exoPlayer.setMediaSource(mediaSource,true)
        exoPlayer.prepare()
        exoPlayer.playWhenReady = true

    }

这是我的缓存助手类(从 OnCreate() 中的 MainActivity 调用):

class MyCacher(private val receivedContext: Context,private val cacheDir: File,private val mpInstanceReceived: MyMediaPlayer ) {

    companion object {
        var simpleCache: SimpleCache? = null
        var leastRecentlyUsedCacheEvictor: LeastRecentlyUsedCacheEvictor? = null
        var exoDatabaseProvider: ExoDatabaseProvider? = null
        var exoPlayerCacheSize: Long = 90 * 1024 * 256
    }

    fun initCacheVariables() { 
        if (leastRecentlyUsedCacheEvictor == null) {
            leastRecentlyUsedCacheEvictor = LeastRecentlyUsedCacheEvictor(exoPlayerCacheSize)
            Log.d(TAG,"initCacheVariables: inside leastRecentlyUsed....")
        }

        if (exoDatabaseProvider == null) {
            exoDatabaseProvider = ExoDatabaseProvider(receivedContext)
            Log.d(TAG,"initCacheVariables: inside exoDatabaseProvider ... ")
        }

        if (simpleCache == null) {
            simpleCache = SimpleCache(cacheDir,leastRecentlyUsedCacheEvictor!!,exoDatabaseProvider!!)
            Log.d(TAG,"initCacheVariables: inside simpleCache..")
        }
        
        mpInstanceReceived.initExoPlayerWithCache()

    }

}

据我所知,ExoPlayer 的缓存使用 RAM,而 Glide 从写入磁盘的缓存中读取。两者如何相互影响对我来说是个谜。 我搜索了一个论坛,但目前没有找到相关主题。

总结:在执行 ExoPlayer 使用 CacheDataSource 流式传输 mp3 后,Glide 的加载速度变慢了(在 6 到 7 行 Recycler 视图上显示缩略图大小图像延迟 2-3 秒)

问题 1:从缓存中播放 ExoPlayer 对我的 Glide 加载速度有何影响?

问题 2:这正常吗?我该怎么做才能将 Glide 恢复到以前的加载速度。

注意)Firestore 上的图片文件大小范围为 200kb-1.0MB(有些文件为了测试目的而故意很大)

  • Android Studio 4.2/Kotlin
  • 测试模拟器:NEXUS 5X API 25

非常感谢!

解决方法

经过大约一周的挣扎(和过度搜索)后,我找到了解决方案。事实证明,ExoPlayer 和 Glide 共享同一个文件夹,SimpleCache 的构造函数正在删除 Glide 的缓存,因为它们是无法识别的文件。

您可以通过为 ExoPlayer 的缓存使用不同的位置(或添加子文件夹)来解决此问题

源链接:https://github.com/bumptech/glide/issues/4429#issuecomment-737870711

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...