ExoPlayer TrackSelectionDialog 比较器不起作用

问题描述 投票:0回答:1

我正在以“height”+“p”格式向用户打印视频轨道。 默认情况下

TrackSelectionDialog
以随机顺序打印它们。我想将它们从最小到最大重新排序。

所以我的

TrackSelectionView
init
中有这个:

/**
 * Initialize the view to select tracks for a specified renderer using {@link MappedTrackInfo} and
 * a set of {@link DefaultTrackSelector.Parameters}.
 *
 * @param mappedTrackInfo The {@link MappedTrackInfo}.
 * @param rendererIndex The index of the renderer.
 * @param isDisabled Whether the renderer should be initially shown as disabled.
 * @param overrides List of initial overrides to be shown for this renderer. There must be at most
 *     one override for each track group. If {@link #setAllowMultipleOverrides(boolean)} hasn't
 *     been set to {@code true}, only the first override is used.
 * @param trackFormatComparator An optional comparator used to determine the display order of the
 *     tracks within each track group.
 * @param listener An optional listener for track selection updates.
 */
public void init(
        MappedTrackInfo mappedTrackInfo,
        int rendererIndex,
        boolean isDisabled,
        List<SelectionOverride> overrides,
        @Nullable Comparator<Format> trackFormatComparator,
        @Nullable TrackSelectionListener listener) {
    this.mappedTrackInfo = mappedTrackInfo;
    this.rendererIndex = rendererIndex;
    this.isDisabled = isDisabled;
    if (trackFormatComparator == null)
        this.trackInfoComparator = null;
    else
        this.trackInfoComparator = new Comparator<TrackInfo>() {
            @Override
            public int compare(TrackInfo o1, TrackInfo o2) {
                return trackFormatComparator.compare(o1.format, o2.format);
            }
        };
    this.listener = listener;
    int maxOverrides = allowMultipleOverrides ? overrides.size() : Math.min(overrides.size(), 1);
    for (int i = 0; i < maxOverrides; i++) {
        SelectionOverride override = overrides.get(i);
        this.overrides.put(override.groupIndex, override);
    }
    updateViews();
}

我已将其添加到我的

TrackSelectionDialog

public void setTrackFormatComparator(@Nullable Comparator<Format> trackFormatComparator) {
        TrackSelectionDialog.trackFormatComparator = trackFormatComparator;
    }

并在

trackFormatComparator
onCreateView
中取消注释
TrackSelectionViewFragment
(在
TrackSelectionDialog
类中):

    trackSelectionView.init(
            mappedTrackInfo,
            rendererIndex,
            isDisabled,
            overrides,
            trackFormatComparator  /*null*/,
            /* listener= */ this);

然后在我的播放器活动中,我写了这个来按帧高度对视频轨道进行排序:

        TrackSelectionDialog trackSelectionDialog = TrackSelectionDialog.createForTrackSelector(
                trackSelector, dismissedDialog -> isShowingTrackSelectionDialog = false);
        trackSelectionDialog.setTrackFormatComparator((o1, o2) -> Math.max(o1.height, o2.height));
        trackSelectionDialog.show(getSupportFragmentManager(), null);

但是什么也没发生。 视频轨道仍默认排序。

我的代码有什么问题或者我忘记了代码的哪一部分?

java android comparator exoplayer
1个回答
0
投票

Java Android 视频问题

比较器: 您编码的

Comparator Logic
(o1, o2) -> Math.max(o1.height, o2.height)
不正确。这个
comparator
实际上提供了比较两种格式时的最大高度,但这不是比较器的正确输出。
comparator
应改为给出指示顺序的
integer
,例如
less than
为负数,
equal
为零,
greater than
为正数。

确保

TrackSelectionDialog
正确通过并正确使用
trackFormatComparator

关于这一点,将代码行替换为:

// Fixed the comparator logic code for sorting by height
trackSelectionDialog.setTrackFormatComparator((o1, o2) -> Integer.compare(o1.height, o2.height));

现在您的

Comparator
应该根据高度比较返回一个整数,这是预期的。

© www.soinside.com 2019 - 2024. All rights reserved.