如何在Exo-player v2.x中添加自适应音轨选择器?

时间:2019-01-01 17:32:27

标签: java android exoplayer mpeg-dash exoplayer2.x

我正在应用中设置Exo Player,并且已经在播放器中实现了DASH自适应流,并希望添加功能以播放用户选择的特定质量的曲目。

我的Exo播放器版本为2.9.3,我尝试通过遵循以下Stackoverflow PostMedium 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类:-

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);
  }
}

我希望获得可用质量类型的列表,并让用户选择其中一种,然后使用所选类型播放其余内容,这个问题可能很广泛,但请给我一些指导。

1 个答案:

答案 0 :(得分:0)

尝试以下代码。需要显示列表时调用以下代码。 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();
                    }