Search in sources :

Example 1 with SelectionOverride

use of androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride in project media by androidx.

the class TrackSelectionDialog method createForTrackSelector.

/**
 * Creates a dialog for a given {@link DefaultTrackSelector}, whose parameters will be
 * automatically updated when tracks are selected.
 *
 * @param trackSelector The {@link DefaultTrackSelector}.
 * @param onDismissListener A {@link DialogInterface.OnDismissListener} to call when the dialog is
 *     dismissed.
 */
public static TrackSelectionDialog createForTrackSelector(DefaultTrackSelector trackSelector, DialogInterface.OnDismissListener onDismissListener) {
    MappedTrackInfo mappedTrackInfo = Assertions.checkNotNull(trackSelector.getCurrentMappedTrackInfo());
    TrackSelectionDialog trackSelectionDialog = new TrackSelectionDialog();
    DefaultTrackSelector.Parameters parameters = trackSelector.getParameters();
    trackSelectionDialog.init(/* titleId= */
    R.string.track_selection_title, mappedTrackInfo, /* initialParameters = */
    parameters, /* allowAdaptiveSelections= */
    true, /* allowMultipleOverrides= */
    false, /* onClickListener= */
    (dialog, which) -> {
        DefaultTrackSelector.ParametersBuilder builder = parameters.buildUpon();
        for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
            builder.clearSelectionOverrides(/* rendererIndex= */
            i).setRendererDisabled(/* rendererIndex= */
            i, trackSelectionDialog.getIsDisabled(/* rendererIndex= */
            i));
            List<SelectionOverride> overrides = trackSelectionDialog.getOverrides(/* rendererIndex= */
            i);
            if (!overrides.isEmpty()) {
                builder.setSelectionOverride(/* rendererIndex= */
                i, mappedTrackInfo.getTrackGroups(/* rendererIndex= */
                i), overrides.get(0));
            }
        }
        trackSelector.setParameters(builder);
    }, onDismissListener);
    return trackSelectionDialog;
}
Also used : SelectionOverride(androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector) MappedTrackInfo(androidx.media3.exoplayer.trackselection.MappingTrackSelector.MappedTrackInfo)

Example 2 with SelectionOverride

use of androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride in project media by androidx.

the class DownloadHelper method addTrackSelectionForSingleRenderer.

/**
 * Convenience method to add a selection of tracks to be downloaded for a single renderer. Must
 * not be called until after preparation completes.
 *
 * @param periodIndex The period index the track selection is added for.
 * @param rendererIndex The renderer index the track selection is added for.
 * @param trackSelectorParameters The {@link DefaultTrackSelector.Parameters} to obtain the new
 *     selection of tracks.
 * @param overrides A list of {@link SelectionOverride SelectionOverrides} to apply to the {@code
 *     trackSelectorParameters}. If empty, {@code trackSelectorParameters} are used as they are.
 */
public void addTrackSelectionForSingleRenderer(int periodIndex, int rendererIndex, DefaultTrackSelector.Parameters trackSelectorParameters, List<SelectionOverride> overrides) {
    assertPreparedWithMedia();
    DefaultTrackSelector.ParametersBuilder builder = trackSelectorParameters.buildUpon();
    for (int i = 0; i < mappedTrackInfos[periodIndex].getRendererCount(); i++) {
        builder.setRendererDisabled(/* rendererIndex= */
        i, /* disabled= */
        i != rendererIndex);
    }
    if (overrides.isEmpty()) {
        addTrackSelection(periodIndex, builder.build());
    } else {
        TrackGroupArray trackGroupArray = mappedTrackInfos[periodIndex].getTrackGroups(rendererIndex);
        for (int i = 0; i < overrides.size(); i++) {
            builder.setSelectionOverride(rendererIndex, trackGroupArray, overrides.get(i));
            addTrackSelection(periodIndex, builder.build());
        }
    }
}
Also used : TrackGroupArray(androidx.media3.common.TrackGroupArray) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector)

Example 3 with SelectionOverride

use of androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride in project media by androidx.

the class TrackSelectionView method 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;
    this.trackInfoComparator = trackFormatComparator == null ? null : (o1, o2) -> 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();
}
Also used : TrackGroup(androidx.media3.common.TrackGroup) Context(android.content.Context) LinearLayout(android.widget.LinearLayout) AttrRes(androidx.annotation.AttrRes) Arrays(java.util.Arrays) MappedTrackInfo(androidx.media3.exoplayer.trackselection.MappingTrackSelector.MappedTrackInfo) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector) RendererCapabilities(androidx.media3.exoplayer.RendererCapabilities) TypedArray(android.content.res.TypedArray) ArrayList(java.util.ArrayList) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) AttributeSet(android.util.AttributeSet) View(android.view.View) Assertions(androidx.media3.common.util.Assertions) SelectionOverride(androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride) UnstableApi(androidx.media3.common.util.UnstableApi) LayoutInflater(android.view.LayoutInflater) TrackGroupArray(androidx.media3.common.TrackGroupArray) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull) Format(androidx.media3.common.Format) SparseArray(android.util.SparseArray) C(androidx.media3.common.C) List(java.util.List) Nullable(androidx.annotation.Nullable) CheckedTextView(android.widget.CheckedTextView) Comparator(java.util.Comparator) SelectionOverride(androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride)

Example 4 with SelectionOverride

use of androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride in project media by androidx.

the class TrackSelectionView method updateViewStates.

private void updateViewStates() {
    disableView.setChecked(isDisabled);
    defaultView.setChecked(!isDisabled && overrides.size() == 0);
    for (int i = 0; i < trackViews.length; i++) {
        SelectionOverride override = overrides.get(i);
        for (int j = 0; j < trackViews[i].length; j++) {
            if (override != null) {
                TrackInfo trackInfo = (TrackInfo) Assertions.checkNotNull(trackViews[i][j].getTag());
                trackViews[i][j].setChecked(override.containsTrack(trackInfo.trackIndex));
            } else {
                trackViews[i][j].setChecked(false);
            }
        }
    }
}
Also used : SelectionOverride(androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride) MappedTrackInfo(androidx.media3.exoplayer.trackselection.MappingTrackSelector.MappedTrackInfo)

Example 5 with SelectionOverride

use of androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride in project media by androidx.

the class TrackSelectionView method onTrackViewClicked.

private void onTrackViewClicked(View view) {
    isDisabled = false;
    TrackInfo trackInfo = (TrackInfo) Assertions.checkNotNull(view.getTag());
    int groupIndex = trackInfo.groupIndex;
    int trackIndex = trackInfo.trackIndex;
    SelectionOverride override = overrides.get(groupIndex);
    Assertions.checkNotNull(mappedTrackInfo);
    if (override == null) {
        // Start new override.
        if (!allowMultipleOverrides && overrides.size() > 0) {
            // Removed other overrides if we don't allow multiple overrides.
            overrides.clear();
        }
        overrides.put(groupIndex, new SelectionOverride(groupIndex, trackIndex));
    } else {
        // An existing override is being modified.
        int overrideLength = override.length;
        int[] overrideTracks = override.tracks;
        boolean isCurrentlySelected = ((CheckedTextView) view).isChecked();
        boolean isAdaptiveAllowed = shouldEnableAdaptiveSelection(groupIndex);
        boolean isUsingCheckBox = isAdaptiveAllowed || shouldEnableMultiGroupSelection();
        if (isCurrentlySelected && isUsingCheckBox) {
            // Remove the track from the override.
            if (overrideLength == 1) {
                // The last track is being removed, so the override becomes empty.
                overrides.remove(groupIndex);
            } else {
                int[] tracks = getTracksRemoving(overrideTracks, trackIndex);
                overrides.put(groupIndex, new SelectionOverride(groupIndex, tracks));
            }
        } else if (!isCurrentlySelected) {
            if (isAdaptiveAllowed) {
                // Add new track to adaptive override.
                int[] tracks = getTracksAdding(overrideTracks, trackIndex);
                overrides.put(groupIndex, new SelectionOverride(groupIndex, tracks));
            } else {
                // Replace existing track in override.
                overrides.put(groupIndex, new SelectionOverride(groupIndex, trackIndex));
            }
        }
    }
}
Also used : SelectionOverride(androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride) MappedTrackInfo(androidx.media3.exoplayer.trackselection.MappingTrackSelector.MappedTrackInfo) CheckedTextView(android.widget.CheckedTextView)

Aggregations

SelectionOverride (androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride)7 TrackGroupArray (androidx.media3.common.TrackGroupArray)5 TrackSelectionOverride (androidx.media3.common.TrackSelectionOverrides.TrackSelectionOverride)4 MappedTrackInfo (androidx.media3.exoplayer.trackselection.MappingTrackSelector.MappedTrackInfo)4 Format (androidx.media3.common.Format)3 DefaultTrackSelector (androidx.media3.exoplayer.trackselection.DefaultTrackSelector)3 Test (org.junit.Test)3 CheckedTextView (android.widget.CheckedTextView)2 Nullable (androidx.annotation.Nullable)2 Context (android.content.Context)1 TypedArray (android.content.res.TypedArray)1 AttributeSet (android.util.AttributeSet)1 SparseArray (android.util.SparseArray)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 LinearLayout (android.widget.LinearLayout)1 AttrRes (androidx.annotation.AttrRes)1 C (androidx.media3.common.C)1 TrackGroup (androidx.media3.common.TrackGroup)1 Assertions (androidx.media3.common.util.Assertions)1