如何在 Exo-player v2.x 中添加 Adaptive Track Selector?

How to add Adaptive Track Selector in Exo-player v2.x?

我正在我的应用程序中设置 Exo Player,并且我在我的播放器中实现了 DASH 自适应流式传输,并希望添加功能来播放用户指定质量 select 的曲目。

我的 Exo 播放器版本是 2.9.3,我尝试按照这些 ,Medium Post 来实现,但是有点混乱,部分代码已被弃用,说实话我是初学者。

这是我初始化播放器的代码:-

private void initializePlayer() {
     if (player == null) {
        bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory factory = new PlayerTrackSelector(bandwidthMeter);
        trackSelector = new DefaultTrackSelector(factory);
        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
        player.addVideoListener(this);
        playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
        player.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
        player.addListener(this);
        playerView.setPlayer(player);
    }
    MediaSource mediaSource = buildMediaSource(Uri.parse("http://192.168.43.238:3000/storage/video-dash/9e351142a4eb1664643bf93ba13959e8.mpd"));
    player.prepare(mediaSource, true, false);
    player.setPlayWhenReady(playWhenReady);
    player.seekTo(currentWindow, playbackPosition);
}

TrackSelector Class:-

public class PlayerTrackSelector implements TrackSelection.Factory {
    private BandwidthMeter bandwidthMeter;

  public PlayerTrackSelector(BandwidthMeter bandwidthMeter) {
    this.bandwidthMeter = bandwidthMeter;
  }

@Override
public TrackSelection createTrackSelection(TrackGroup group, BandwidthMeter bandwidthMeter, int... tracks) {

    return new AdaptiveTrackSelection(group,tracks,bandwidthMeter,
            AdaptiveTrackSelection.DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS,
            AdaptiveTrackSelection.DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS,
            AdaptiveTrackSelection.DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS,
            AdaptiveTrackSelection.DEFAULT_BANDWIDTH_FRACTION,
            AdaptiveTrackSelection.DEFAULT_BUFFERED_FRACTION_TO_LIVE_EDGE_FOR_QUALITY_INCREASE,
            AdaptiveTrackSelection.DEFAULT_MIN_TIME_BETWEEN_BUFFER_REEVALUTATION_MS,
            Clock.DEFAULT);
  }
}

我希望获得可用质量类型的列表并让用户 select 其中之一并使用 selected 类型播放其余内容,这个问题可能很宽泛但是请稍微指导一下。

试试下面的代码。当您需要显示列表时调用以下代码。 rendererIndex -> 你可以手动尝试。 TRACK_TYPE_AUDIO = 1,TRACK_TYPE_VIDEO = 2。

更新:

MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
                    if (mappedTrackInfo != null) {
                        int rendererIndex = 2;
                        int rendererType = mappedTrackInfo.getRendererType(rendererIndex);
                        boolean allowAdaptiveSelections =
                                rendererType == C.TRACK_TYPE_VIDEO
                                        || (rendererType == C.TRACK_TYPE_AUDIO
                                        && mappedTrackInfo.getTypeSupport(C.TRACK_TYPE_VIDEO)
                                        == MappingTrackSelector.MappedTrackInfo.RENDERER_SUPPORT_NO_TRACKS);
                        Pair<AlertDialog, TrackSelectionView> dialogPair =
                                TrackSelectionView.getDialog(xxxxxxxxxxxxxx.this, "Track Selector", trackSelector, rendererIndex);
                        dialogPair.second.setShowDisableOption(true);
                        dialogPair.second.setAllowAdaptiveSelections(allowAdaptiveSelections);
                        dialogPair.first.show();
                    }