Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent re-buffering on changing max bit rate in ExoPlayer

I am using setMaxBitrate provided by DefaultTrackSelector to set max bit rate when user changes video quality.

val parameters = defaultTrackSelector.buildUponParameters()
            .setMaxVideoBitrate(bitrate)
            .build()

defaultTrackSelector.parameters = parameters

But as soon as this function is called, the current buffer is discarded & re-buffering is shown right away. Is there any way to keep playing using old buffer & just load the new buffer using the new bitrate settings like YouTube does?

This issue has been discussed here: https://github.com/google/ExoPlayer/issues/3522 https://github.com/google/ExoPlayer/issues/2250

But there doesn't seem to be any solution yet. Any help regarding this issue would be appreciated. Thanks in advance.

like image 453
Abhishek V Avatar asked Jan 20 '20 09:01

Abhishek V


1 Answers

Easily you can do it.You have to use ExoPlayer already and ExoPlayer is provide a seekTo() method.

On this method,You should pass only player current position at which point you stopped before.

Step:-1 You have to change your Quality like 144p to 720p. on this Changing time you have to store your current ExoPlayer current position used this method:-

Private int currentPosition=player.getCurrentPosition();

Step -2 After you have to build your exoplayer media source:-

// Measures bandwidth during playback. Can be null if not required.
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, getString(R.string.app_name)), bandwidthMeter);
        // This is the MediaSource representing the media to be played.
        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource("Pass Your Video Url");
        // Prepare the player with the source.
        player.prepare(videoSource);

Step 3:- check this condition

if (this.currentPosition > 0) {
        player.seekTo(this.currentPosition);
        this.currentPosition = 0;
        player.setPlayWhenReady(true);
    } else {
        player.setPlayWhenReady(true);
    }

and it's work good you have to watch your video in where are you left.

Step 4:- If your quality his not good that time used is method.

public int getWifiLevel()
{
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int linkSpeed = wifiManager.getConnectionInfo().getRssi();
    int level = WifiManager.calculateSignalLevel(linkSpeed, 5);
    return level;
}

Based on wifi level or link speed you can decide if it has the low connection or high connection internet.

like image 81
Pritesh Parmar Avatar answered Oct 21 '22 09:10

Pritesh Parmar