Search code examples
androidkotlincachingandroid-glideexoplayer

Glide slow loading after Exoplayer Cache enabled


first time writing here on stackoverflow. (You bet I'm a noob in Android development)

I've been experimenting with a quasi-Spotify clone app that has a Recyclerview showing song thumbnails & load mp3s from Firestore db via URL. The app is displaying images using Glide and plays mp3 using ExoPlayer.

All is working fine except the loading of images (about 12 of them currently) got a bit slower after I've enabled ExoPlayer to play using Cache. Before implementing Cache for ExoPlayer Glide displayed image immediately upon launch (less than 1 second) but after using Cache for ExoPlayer it takes about 3~4 seconds to display 6~7 rows of Recyclerview.

ExoPlayer prep BEFORE using 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 prep AFTER using 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

    }

And this is my Caching helper class (called from MainActivity inside OnCreate()):

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()

    }

}

As far as I understand, ExoPlayer's caching uses RAM while Glide reads from cache written on the Disk. How both are affected by each other is a mystery to me. I've searched a forum but found no related topics so far.

SUMMARY: After executing ExoPlayer to stream mp3 with CacheDataSource, Glide's loading speed got way slower (2-3 seconds delay to display thumbnail size image on 6 to 7 rows of Recycler view)

QUESTION1: How is ExoPlayer's play from cache affecting my Glide loading speed?

QUESTION2: Is this normal? What should I do to revert my Glide to have its previous loading speed.

Note) Size of the image files on Firestore are range from 200kb-1.0MB (some files are intentionally large for testing purposes)

  • Android Studio 4.2/Kotlin
  • Test emulator: NEXUS 5X API 25

Thanks so much in advance!


Solution

  • After struggling (and excessive searching) for about a week, I've found a solution to this. It turns out that ExoPlayer and Glide were sharing the same folder and SimpleCache's constructor was deleting Glide's cache as they were unrecognized files.

    You can solve this issue by using a different location for your ExoPlayer's cache (or adding a subfolder)

    Source link: https://github.com/bumptech/glide/issues/4429#issuecomment-737870711