
Posted by Mayuri Khinvasara Khabya – Developer Relations Engineer (LinkedIn and X)
In in the present day’s media-centric apps, delivering a easy, uninterrupted playback expertise is vital to a pleasant consumer expertise. Customers count on their movies to begin immediately and play seamlessly with out pauses.
The core problem is latency. Historically, a video participant solely begins its work—connecting, downloading, parsing, buffering—after the consumer has chosen an merchandise for playback. This reactive method is gradual for in the present day’s brief type video context. The answer is to be proactive. We have to anticipate what the consumer will watch subsequent and get the content material prepared forward of time. That is the essence of preloading.
The important thing advantages of preloading embrace:
- 🚀 Quicker Playback Begin: Movies are already able to go, resulting in faster transitions between gadgets and a extra speedy begin.
- 📉 Lowered Buffering: By proactively loading knowledge, playback is way much less more likely to stall, for instance because of community hiccups.
- ✨ Ensuing smoother Person Expertise: The mixture of sooner begins and fewer buffering creates a extra fluid, seamless interplay for customers to get pleasure from.
On this three-part collection, we’ll introduce and deep dive into Media3’s highly effective utilities for (pre)loading parts.
- In Half 1, we’ll cowl the foundations: understanding the totally different preloading methods obtainable in Media3, enabling PreloadConfiguration and establishing the DefaultPreloadManager, enabling your app to preload gadgets. By the tip of this weblog, it is best to have the ability to preload and play media gadgets along with your configured rating and length.
- In Half 2, we’ll get into extra superior matters of DefaultPreloadManager: utilizing listeners for analytics, exploring production-ready greatest practices just like the sliding window sample and customized shared parts of DefaultPreloadManager and ExoPlayer.
- In Half 3, we’ll dive deep into disk caching with DefaultPreloadManager.
Preloading to the rescue! 🦸♀️
The core concept behind preloading is straightforward: load media content material earlier than you want it. By the point a consumer swipes to the following video, the primary segments of the video are already downloaded and obtainable, prepared for speedy playback.
Consider it like a restaurant. A busy kitchen would not watch for an order to begin chopping onions. 🧅 They do their prep work upfront. Preloading is the prep work in your video participant.
When enabled, preloading may help decrease be a part of latency when a consumer skips to the following merchandise earlier than the playback buffer reaches the following merchandise. The primary interval of the following window is ready and video, audio and textual content samples are buffered. The preloaded interval is later queued into the participant with buffered samples instantly obtainable and able to be fed to the codec for rendering.
In Media3 there are two major APIs for preloading, every suited to totally different use instances. Selecting the best API is step one.
1. Preloading playlist gadgets with PreloadConfiguration
That is the easy method, helpful for linear, sequential media like playlists the place the playback order is predictable (like a collection of episodes). You give the participant the complete listing of media gadgets utilizing ExoPlayer’s playlist APIs and set the PreloadConfiguration for the participant, then it routinely preloads the following gadgets within the sequence as configured. This API makes an attempt to optimize the be a part of latency when a consumer skips to the following merchandise earlier than the playback buffer already overlaps into the following merchandise.
Preloading is just began when no media is being loaded for the continued playback, which prevents it from competing for bandwidth with the first playback.
If you happen to’re nonetheless unsure whether or not you want preloading, this API is a superb low-lift choice to strive it out!
participant.preloadConfiguration = PreloadConfiguration(/* targetPreloadDurationUs= */ 5_000_000L)
With the PreloadConfiguration above, the participant tries to preload 5 seconds of media for the following merchandise within the playlist.
As soon as opted-in, playlist preloading will be turned off once more by utilizing PreloadConfiguration.DEFAULT to disable playlist preloading:
participant.preloadConfiguration = PreloadConfiguration.DEFAULT
2. Preloading dynamic lists with PreloadManager
For dynamic UIs like vertical feeds or carousels, the place the “subsequent” merchandise is decided by consumer interplay, the PreloadManager API is suitable. It is a new highly effective, standalone part inside the Media3 ExoPlayer library particularly designed to proactively preload. It manages a set of potential MediaSources, prioritizing them primarily based on proximity to the consumer’s present place and affords granular management over what to preload, appropriate for advanced situations like dynamic feeds of brief type movies.
Setting Up Your PreloadManager
The DefaultPreloadManager is the canonical implementation for PreloadManager.
The builder of DefaultPreloadManager can construct each the DefaultPreloadManager and any ExoPlayer cases that can play its preloaded content material. To create a DefaultPreloadManager, you will have to go a TargetPreloadStatusControl, which the preload supervisor can question to learn the way a lot to load for an merchandise. We are going to clarify and outline an instance of TargetPreloadStatusControl within the part beneath.
val preloadManagerBuilder = DefaultPreloadManager.Builder(context, targetPreloadStatusControl) val preloadManager = val preloadManagerBuilder.construct() // Construct ExoPlayer with DefaultPreloadManager.Builder val participant = preloadManagerBuilder.buildExoPlayer()
Utilizing the identical builder for each the ExoPlayer and DefaultPreloadManager is critical, which ensures that the parts underneath the hood of them are appropriately shared.
And that is it! You now have a supervisor able to obtain directions.
Configuring Period and Rating with TargetPreloadStatusControl
What if you wish to preload, say, 10 seconds of video ? You may present the place of your media gadgets within the carousel, and the DefaultPreloadManager prioritizes loading the gadgets primarily based on how shut it’s to the merchandise the consumer is presently enjoying.
If you wish to management how a lot length of the merchandise to preload, you possibly can inform that with DefaultPreloadManager.PreloadStatus you come back.
For instance,
- Merchandise ‘A’ is the very best precedence, load 5 seconds of video.
- Merchandise ‘B’ is medium precedence however if you get to it, load 3 seconds of video.
- Merchandise ‘C’ is much less precedence, load solely tracks.
- Merchandise ‘D’ is even much less of a precedence, simply put together.
- Every other gadgets are distant, Don’t preload something.
This granular management may help you optimize your useful resource utilization which is really helpful for a seamless playback.
import androidx.media3.exoplayer.DefaultPreloadManager.PreloadStatus class MyTargetPreloadStatusControl( currentPlayingIndex: Int = C.INDEX_UNSET ) : TargetPreloadStatusControl<Int,PreloadStatus> { // The app is answerable for updating this primarily based on UI state override enjoyable getTargetPreloadStatus(index: Int): PreloadStatus? { val distance = index - currentPlayingIndex // Adjoining gadgets (Subsequent): preload 5 seconds if (distance == 1) { // Return a PreloadStatus that's labelled by STAGE_SPECIFIED_RANGE_LOADED and recommend loading // 5000ms from the default begin place return PreloadStatus.specifiedRangeLoaded(5000L) } // Adjoining gadgets (Earlier): preload 3 seconds else if (distance == -1) { // Return a PreloadStatus that's labelled by STAGE_SPECIFIED_RANGE_LOADED //and recommend loading 3000ms from the default begin place return PreloadStatus.specifiedRangeLoaded(3000L) } // Objects two positions away: simply choose tracks else if (distance) == 2) { // Return a PreloadStatus that's labelled by STAGE_TRACKS_SELECTED return PreloadStatus.TRACKS_SELECTED } // Objects 4 positions away: simply choose put together else if (abs(distance) <= 4) { // Return a PreloadStatus that's labelled by STAGE_SOURCE_PREPARED return PreloadStatus.SOURCE_PREPARED } // All different gadgets are too distant return null } }
Tip: PreloadManager can maintain each the earlier and subsequent gadgets preloaded, whereas the PreloadConfiguration will solely sit up for the following gadgets.
Managing Preloading Objects
Together with your supervisor created, you can begin telling it what to work on. As your consumer scrolls by a feed, you will establish the upcoming movies and add them to the supervisor. The interplay with the PreloadManager is a state-driven dialog between your UI and the preloading engine.
1. Add Media Objects
As you populate your feed, you could inform the supervisor of the media it wants to trace. If you’re beginning, you could possibly add your complete listing you need to preload. Subsequently you possibly can maintain including a single merchandise to the listing as and when required. You may have full management over what gadgets are within the preloading listing which suggests you additionally need to handle what’s added and faraway from the supervisor.
val initialMediaItems = pullMediaItemsFromService(/* depend= */ 20) for (index in 0 till initialMediaItems.dimension) { preloadManager.add( initialMediaItems.get(index),index) ) }
The supervisor will now begin fetching knowledge for this MediaItem within the background.
After including, inform the supervisor to re-evaluate its new listing (hinting that one thing has modified like including/ eradicating an merchandise, or the consumer switches to play a brand new merchandise.)
preloadManager.invalidate()
2. Retrieve and Play an Merchandise
Right here comes the principle playback logic. When the consumer decides to play that video, you need not create a brand new MediaSource. As a substitute, you ask the PreloadManager for the one it has already ready. You may retrieve the MediaSource from the Preload Supervisor utilizing the MediaItem.
If the retrieved merchandise from the PreloadManager is null, meaning the mediaItem will not be preloaded but or added to the PreloadMamager, so that you select to set the mediaItem immediately.
// When a media merchandise is about to display on the display val mediaSource = preloadManager.getMediaSource(mediaItem) if (mediaSource!= null) { participant.setMediaSource(mediaSource) } else { // If mediaSource is null, that mediaItem hasn't been added but. // So, ship it on to the participant. participant.setMediaItem(mediaItem) } participant.put together() // When the media merchandise is displaying on the heart of the display participant.play()
By getting ready the MediaSource retrieved from the PreloadManager, you seamlessly transition from preloading to playback, utilizing the info that is already in reminiscence. That is what makes the beginning time sooner.
3. Preserve the present index in sync with the UI
Since our feed / listing might be dynamic, it is necessary to inform the PreloadManager of your present enjoying index in order that it may all the time prioritize gadgets nearest to your present index for preloading.
preloadManager.setCurrentPlayingIndex(currentIndex) // Have to name invalidate() to replace the priorities preloadManager.invalidate()
4. Take away an Merchandise
To maintain the supervisor environment friendly, it is best to take away gadgets it not wants to trace, reminiscent of gadgets which might be distant from the consumer’s present place.
// When an merchandise is simply too removed from the present enjoying index preloadManager.take away(mediaItem)
If it’s good to clear all gadgets directly, you possibly can name preloadManager.reset().
5. Launch the Supervisor
Once you not want the PreloadManager (e.g., when your UI is destroyed), you could launch it to liberate its assets. A superb place to do that is the place you’re already releasing your Participant’s assets. It’s really helpful to launch the supervisor earlier than the participant because the participant can proceed to play if you happen to do not want any extra preloading.
// In your Exercise's onDestroy() or Composable's onDispose preloadManager.launch()
Demo time
Examine it stay in motion 👍
Within the demo beneath , we see the impression of PreloadManager on the precise aspect which has sooner load occasions, whereas the left aspect reveals the prevailing expertise. You too can view the code pattern for the demo. (Bonus: It additionally shows startup latency for each video)
![Jetpack Media3 API for fast loading of short videos [PreloadManager]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPRidTRe3s-2TIek6_YlfUsFwxRAaH-6rynAMXLX7UwVM9b-PhCAfOqAWwrTIBj6pSfntZizlYLU6nhmkNqbwwvBLGkWRjsOKNb-c56Jr8gutxBkSZidmOP1T420tBU5knaVvuAygMKHnDV8epXMc2TuIZN2klfnIKulNQUncwfz4fuuIlbLQ3yxhrhrU/s1600/Demo-PreloadManager%202.gif)
What’s Subsequent?
And that is a wrap for Half 1! You now have the instruments to construct a dynamic preloading system. You may both use PreloadConfiguration to preload the following merchandise of a playlist in ExoPlayer or arrange a DefaultPreloadManager, add and take away gadgets on the fly, configure the goal preload standing, and appropriately retrieve the preloaded content material for playback.
In Half 2, we’ll go deeper on the DefaultPreloadManager. We’ll discover the right way to pay attention for preloading occasions, focus on greatest practices like utilizing a sliding window to keep away from reminiscence points, and peek underneath the hood at customized shared parts of ExoPlayer and DefaultPreloadManager.
Do you will have any suggestions to share? We’re keen to listen to from you.
Keep tuned, and go make your app sooner! 🚀

