我正在使用setMaxBitrate提供的DefaultTrackSelector来设置用户更改视频质量时的最大比特率。

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

defaultTrackSelector.parameters = parameters

但是,一旦调用此函数,将立即丢弃当前缓冲区并立即显示重新缓冲。有什么方法可以像旧YouTube一样继续使用旧缓冲区播放视频并仅使用新的比特率设置加载新缓冲区吗?

此问题已在此处讨论:
https://github.com/google/ExoPlayer/issues/3522
https://github.com/google/ExoPlayer/issues/2250

但是似乎还没有任何解决方案。任何有关此问题的帮助将不胜感激。提前致谢。

最佳答案

您可以轻松地做到这一点。您必须已经使用ExoPlayer,并且ExoPlayer提供了seekTo()方法。

使用此方法时,您只应传递玩家之前停止的当前位置。

步骤:-1
您必须将144p之类的质量更改为720p。在此更改时间上,您必须使用以下方法存储当前的ExoPlayer当前位置:-

Private int currentPosition=player.getCurrentPosition();

步骤-2
在您必须构建exoplayer媒体源之后:-
// 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);

步骤3:-
检查这种情况
if (this.currentPosition > 0) {
        player.seekTo(this.currentPosition);
        this.currentPosition = 0;
        player.setPlayWhenReady(true);
    } else {
        player.setPlayWhenReady(true);
    }

这样很好,您必须在剩下的地方观看视频。

步骤4:-
如果您的质量不好,那用的时间就是方法。
public int getWifiLevel()
{
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int linkSpeed = wifiManager.getConnectionInfo().getRssi();
    int level = WifiManager.calculateSignalLevel(linkSpeed, 5);
    return level;
}

根据wifi级别或链接速度,您可以确定它的连接速度低还是Internet连接速度高。

10-02 02:15
查看更多