Set max bitrate on ExoPlayer

Image for post
Image for post
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.

Image for post
Image for post
Image for post
Image for post

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.

Image for post
Image for post

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.


Written by

Android Engineer at AbemaTV Github:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store