【发布时间】:2021-08-19 07:23:40
【问题描述】:
第一次在stackoverflow上写。 (你敢打赌我是 Android 开发的菜鸟)
我一直在试验一个准 Spotify 克隆应用程序,它有一个 Recyclerview 显示歌曲缩略图并通过 URL 从 Firestore db 加载 mp3。该应用使用 Glide 显示图像并使用 ExoPlayer 播放 mp3。
在我启用 ExoPlayer 使用缓存播放之后,除了加载图像(目前大约 12 个)有点慢之外,一切都工作正常。在使用 Cache for ExoPlayer 之前 Glide 启动后立即显示图像(不到 1 秒),但在使用 Cache for ExoPlayer 后,大约需要 3~4 秒才能显示 6~7 行 Recyclerview。
使用 cacheDataSoruce 之前的 ExoPlayer 准备
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
}
使用 cacheDataSoruce 之后的 ExoPlayer 准备
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 从写入磁盘的缓存中读取。两者如何相互影响对我来说是一个谜。 我搜索了一个论坛,但目前没有找到相关主题。
总结:在使用 CacheDataSource 执行 ExoPlayer 流式传输 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
提前非常感谢!
【问题讨论】:
标签: android kotlin caching android-glide exoplayer