Set max bitrate on ExoPlayer

Taku Semba
Jul 21, 2019 · 2 min read
set max bitrate on ExoPlayer

Sometimes, you may have some situation where you want to limit video resolution, for example, when a user explicitly set the max resolution from the setting or when a playback surface size is too small and you do not need to get the higher resolutions.

Set Max Bitrate

ExoPlayer provides a way of setting a max bitrate using DefaultTrackSelector#Parameters. You can use setMaxVideoBitrate or setMaxVideoSize to limit the max bitrate.

setMaxVideoBitrate
setMaxVideoSize

In this way, it works in many cases.

What I mean by “it works in many cases” is that it does not work when you change the max bitrate during the playback, because ExoPlayer discards buffers and decoder reinitialization happens when you change the parameters. So if users could change the max bitrate while they play some video or playback surface size could shrink/expands and you want to change the max bitrate as the playback surface size changes, you would have users wait until ExoPlayer is ready for the new parameters.

Set Max Bitrate Dynamically

So, how do you set a max bitrate while playback goes on? One way you could do is to extend AdaptiveTrackSelection and override thecanSelectFormat method instead of using DefaultTrackSelector#Parameters.

LimitTrackSelection

canSelectFormat would be called when ExoPlayer tries to select the resolution and you can return false whenever ExoPlayer is trying to get unwanted resolutions.

Check this out for the entire implementation.

https://github.com/TakuSemba/ExoPlayer/tree/set-max-bitrate

References

Taku Semba

Written by

Android Engineer at AbemaTV Github: https://github.com/takusemba

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade