3

Glide is a library used to load images in various ways, but I never saw an example of loading other kinds of contents, and passing it forward to other libraries.

As an example, I wonder if Glide could manage videos for ExoPlayer and JSON files for Lottie animations.

I tried to ask it on Glide's repository page (here), but none told me if it's possible, and how .

We could have a cache for images, a cache for videos, and a cache for json files of Lottie, for example.

Is it possible? If so, how?

TylerH
  • 20,799
  • 66
  • 75
  • 101
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • No. Glide is meant to manage image, exoplayer does have its own cache mechanism. lottie too. – Geo Aug 14 '18 at 14:02
  • @GeorgePJ Are you sure about the caching of Lottie and Exoplayer? I don't see it anywhere. I'm talking about caching of actual files from URLs, like on Glide. Not caching within the current video/animation. – android developer Aug 14 '18 at 14:31

2 Answers2

4

For Lottie you need these 3 classes:

@GlideModule
class MedisafeGlideModule : AppGlideModule() {
    override fun registerComponents(
            context: Context, glide: Glide, registry: Registry) {
        registry
                .register(LottieComposition::class.java, LottieDrawable::class.java, LottieDrawableTranscoder())
                .append(InputStream::class.java, LottieComposition::class.java, LottieDecoder())
    }

    override fun isManifestParsingEnabled() = false
}

Second one:

class LottieDecoder : ResourceDecoder<InputStream, LottieComposition> {

    override fun handles(source: InputStream, options: Options): Boolean = true

    @Throws(IOException::class)
    override fun decode(source: InputStream, width: Int, height: Int, options: Options): Resource<LottieComposition> {
        return try {
            val lottieResult = LottieCompositionFactory.fromJsonInputStreamSync(source, null)
            SimpleResource(lottieResult.value!!)
        } catch (ex: Exception) {
            throw IOException("Cannot load lottie from stream", ex)
        }
    }
}

Third one:

class LottieDrawableTranscoder : ResourceTranscoder<LottieComposition, LottieDrawable> {
    override fun transcode(toTranscode: Resource<LottieComposition>, options: Options): Resource<LottieDrawable> {
        val composition = toTranscode.get()
        val lottieDrawable = LottieDrawable()
        lottieDrawable.composition = composition
        lottieDrawable.repeatCount = ValueAnimator.INFINITE
        return SimpleResource(lottieDrawable)
    }
}

How to use:

Glide.with(imageView)
    .`as`(LottieDrawable::class.java)
    .load(url)
    .into(imageView)
1

From Lottie Doc : http://airbnb.io/lottie/android/android.html#caching-animations

Caching Animations

by default. Default cache keys will be created for animations loaded from res/raw/ or assets/. Other APIs require setting a cache key. If you fire multiple animation requests for the same animation in parallel such as a wishlist heart in a RecyclerView, subsequent requests will join the existing task so it only gets parsed once (Lottie >= 2.6.0).All Lottie animations are cached with a LRU cache

To know more about exoplayer cache please take a look at:

https://medium.com/google-exoplayer/pre-caching-downloading-progressive-streams-in-exoplayer-3a816c75e8f6

Using cache in ExoPlayer

Geo
  • 746
  • 6
  • 14
  • 1
    About Lottie, caching from built in animations within the app doesn't help here as it will cache things in memory alone (files already exist). I'm talking about caching in the way Glide can, from URLs. About ExoPlayer, can you please show an example of how to use it, here on StackOverflow? – android developer Aug 14 '18 at 14:59