Search code examples
androidexoplayer

How can I increase ExoPlayer's buffering time?


I'm using ExoPlayer to play videos/live streams on Android. If there is a problem with the connection (connection loss/reduced speed/etc.), the player goes into the buffering state - STATE_BUFFERING, trying to load data. After a couple of seconds it goes to STATE_IDLE. And if the connection is back and the player is in STATE_IDLE, it will not start playing. If the connection is restored and the player is in STATE_BUFFERING, it will load the data and start playing. How to change the buffering timeout? To make it as big as possible, to make ExoPlayer wait for data as long as possible.

ExoPlayer initialization:

exoPlayer = ExoPlayer.Builder(context)
            .build()

Data source initialization:

val dataSourceFactory = DefaultHttpDataSource.Factory()
val mediaSource = HlsMediaSource.Factory(dataSourceFactory)
        .createMediaSource(link)

exoPlayer?.setMediaSource(mediaSource)
exoPlayer?.playWhenReady = false
exoPlayer?.prepare()

Solution

  • You need to customize LoadErrorHandlingPolicy to change the number of retries and the delay between each try. Exoplayer uses DefaultLoadErrorHandlingPolicy as a default implementation which retries for 3 times and after that propagates the error.

    This link can be handy for customizing DefaultLoadErrorHandlingPolicy to better fit your needs.