ExoPlayer icon indicating copy to clipboard operation
ExoPlayer copied to clipboard

How to pre-load the data

Open syg4806 opened this issue 3 years ago • 5 comments

  • Prefetch/Precache: Download the media to local storage as a cache before preparing to play
  • Preload: Load the data from upstream(Cache or Http) into the Player buffer.

We use the list of players with recycler view like youtube shorts. To improve the performance we use prefetch, and we trying to do preload as well.

If we do preload, we need to use at least two players. One is now showing, and another one is for the next video. The second player will preload the media via calling setMediaSource() and prepare() to exoplayer.

  • Do you think the preload is a good option to show the video asap to the user? Because after we do this, the decoder exception has occurred. I guess it's because of the hardware decoder limits depending on the device...
  • Is it correct? If so, will you not recommend doing preload as like I said above?

syg4806 avatar Sep 08 '22 08:09 syg4806

Your current approach of using two independent players is likely required if you ever want both videos to play simultaneously (e.g. while transitioning between the two). The only way to achieve this is with two decoder instances. If the device you're running on doesn't allow two decoders to be instantiated then this isn't possible.

If you want to smoothly transition with a single player instance (pausing the previous video and starting the next video as quickly as possible) you need to make sure:

  1. The start of the next item is 'preloaded' into memory (using your terminology defined above).
  2. The same decoder can be used for both items

(1) is tracked by https://github.com/google/ExoPlayer/issues/3327 (2) is up to you when you prepare the content - the simplest way to ensure the same decoder can be used is to keep all the content the same resolution & codec.

icbaker avatar Sep 08 '22 16:09 icbaker

Thanks for the kind explanation!

We don't want to play both at the same time, we want to preload the next video(2) into the next player(2) buffer while video(1) is playing on the player(1). Are you saying that just preloading the next video(2) into another player(2) instance requires a decoder?

ExoPlayer needs a decoder when rendering the buffered media. Isn't it? We expect the decoder to be used in the rendering stage. In other words, Calling setMediaSource won't need a decoder, but only starts buffering. Do I understand correctly?

My actual question is, On a device that has only one decoder, when a player(1) pauses with no release, and a player(2) starts playing, will there be occured a decoder exception?

syg4806 avatar Sep 11 '22 03:09 syg4806

Are you saying that just preloading the next video(2) into another player(2) instance requires a decoder?

ExoPlayer's default behaviour when you call Player.prepare() is to prepare all the resources required for playback. That includes both buffering the media and acquiring all decoders required. So although not required, it is what I would expect to happen based on what you describe.

We expect the decoder to be used in the rendering stage. In other words, Calling setMediaSource won't need a decoder, but only starts buffering. Do I understand correctly?

Not quite - calling setMediaSource() will do either:

  1. Neither buffer media nor acquire decoders if the player hasn't been prepared (and calling prepare() later will cause both buffering and decoder acquisitiong)
  2. Buffer the media and acquire the decoders if the player has been prepared

The library has this behavior because we assume most users want to start playback as soon as possible after calling prepare(), and decoder acquisition would otherwise delay this.


Your use-case seems like a better fit for using a single player instance, because that makes it easier for the library to understand that only one piece of content can be played at a time (and therefore it can be smarter about when to acquire decoders) - I would recommend experimenting with that approach, and maintaining your different videos as different items in the player's playlist.

icbaker avatar Sep 12 '22 06:09 icbaker

Ok I got it.

I want to make sure one thing. Even if the playWhenReady is false though, it will acquire a decoder, right?

syg4806 avatar Sep 13 '22 01:09 syg4806

Even if the playWhenReady is false though, it will acquire a decoder, right?

That's correct. The value of playWhenReady has no effect on the 'state' of the player (idle, buffering, ready or ended) - it simply means that when the player enters the 'ready' state then playback will start.

icbaker avatar Sep 15 '22 15:09 icbaker

Closing due to inactivity

icbaker avatar Oct 05 '22 17:10 icbaker