Search in sources :

Example 1 with DefaultTrackSelector

use of androidx.media3.exoplayer.trackselection.DefaultTrackSelector 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 DefaultTrackSelector

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

the class ExoPlayerTest method setPlaybackSpeedConsecutivelyNotifiesListenerForEveryChangeOnceAndIsMasked.

@Test
public void setPlaybackSpeedConsecutivelyNotifiesListenerForEveryChangeOnceAndIsMasked() throws Exception {
    List<Float> maskedPlaybackSpeeds = new ArrayList<>();
    Action getPlaybackSpeedAction = new Action("getPlaybackSpeed", /* description= */
    null) {

        @Override
        protected void doActionImpl(ExoPlayer player, DefaultTrackSelector trackSelector, @Nullable Surface surface) {
            maskedPlaybackSpeeds.add(player.getPlaybackParameters().speed);
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).setPlaybackParameters(new PlaybackParameters(/* speed= */
    1.1f)).apply(getPlaybackSpeedAction).setPlaybackParameters(new PlaybackParameters(/* speed= */
    1.2f)).apply(getPlaybackSpeedAction).setPlaybackParameters(new PlaybackParameters(/* speed= */
    1.3f)).apply(getPlaybackSpeedAction).play().build();
    List<Float> reportedPlaybackSpeeds = new ArrayList<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
            reportedPlaybackSpeeds.add(playbackParameters.speed);
        }
    };
    new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).setPlayerListener(listener).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(reportedPlaybackSpeeds).containsExactly(1.1f, 1.2f, 1.3f).inOrder();
    assertThat(maskedPlaybackSpeeds).isEqualTo(reportedPlaybackSpeeds);
}
Also used : Action(androidx.media3.test.utils.Action) Player(androidx.media3.common.Player) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Listener(androidx.media3.common.Player.Listener) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) ActionSchedule(androidx.media3.test.utils.ActionSchedule) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) ArrayList(java.util.ArrayList) Surface(android.view.Surface) ArgumentMatchers.anyFloat(org.mockito.ArgumentMatchers.anyFloat) Listener(androidx.media3.common.Player.Listener) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Nullable(androidx.annotation.Nullable) PlaybackParameters(androidx.media3.common.PlaybackParameters) Test(org.junit.Test)

Example 3 with DefaultTrackSelector

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

the class PlayerActivity method initializePlayer.

/**
 * @return Whether initialization was successful.
 */
protected boolean initializePlayer() {
    if (player == null) {
        Intent intent = getIntent();
        mediaItems = createMediaItems(intent);
        if (mediaItems.isEmpty()) {
            return false;
        }
        boolean preferExtensionDecoders = intent.getBooleanExtra(IntentUtil.PREFER_EXTENSION_DECODERS_EXTRA, false);
        RenderersFactory renderersFactory = DemoUtil.buildRenderersFactory(/* context= */
        this, preferExtensionDecoders);
        trackSelector = new DefaultTrackSelector(/* context= */
        this);
        lastSeenTracksInfo = TracksInfo.EMPTY;
        player = new ExoPlayer.Builder(/* context= */
        this).setRenderersFactory(renderersFactory).setMediaSourceFactory(createMediaSourceFactory()).setTrackSelector(trackSelector).build();
        player.setTrackSelectionParameters(trackSelectionParameters);
        player.addListener(new PlayerEventListener());
        player.addAnalyticsListener(new EventLogger(trackSelector));
        player.setAudioAttributes(AudioAttributes.DEFAULT, /* handleAudioFocus= */
        true);
        player.setPlayWhenReady(startAutoPlay);
        playerView.setPlayer(player);
        serverSideAdsLoader.setPlayer(player);
        debugViewHelper = new DebugTextViewHelper(player, debugTextView);
        debugViewHelper.start();
    }
    boolean haveStartPosition = startItemIndex != C.INDEX_UNSET;
    if (haveStartPosition) {
        player.seekTo(startItemIndex, startPosition);
    }
    player.setMediaItems(mediaItems, /* resetPosition= */
    !haveStartPosition);
    player.prepare();
    updateButtonVisibility();
    return true;
}
Also used : DebugTextViewHelper(androidx.media3.exoplayer.util.DebugTextViewHelper) EventLogger(androidx.media3.exoplayer.util.EventLogger) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector) Intent(android.content.Intent) RenderersFactory(androidx.media3.exoplayer.RenderersFactory) ExoPlayer(androidx.media3.exoplayer.ExoPlayer)

Example 4 with DefaultTrackSelector

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

the class Transformer method startTransformation.

private void startTransformation(MediaItem mediaItem, Muxer muxer) {
    verifyApplicationThread();
    if (player != null) {
        throw new IllegalStateException("There is already a transformation in progress.");
    }
    MuxerWrapper muxerWrapper = new MuxerWrapper(muxer, muxerFactory, containerMimeType);
    this.muxerWrapper = muxerWrapper;
    DefaultTrackSelector trackSelector = new DefaultTrackSelector(context);
    trackSelector.setParameters(new DefaultTrackSelector.ParametersBuilder(context).setForceHighestSupportedBitrate(true).build());
    // Arbitrarily decrease buffers for playback so that samples start being sent earlier to the
    // muxer (rebuffers are less problematic for the transformation use case).
    DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(DEFAULT_MIN_BUFFER_MS, DEFAULT_MAX_BUFFER_MS, DEFAULT_BUFFER_FOR_PLAYBACK_MS / 10, DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS / 10).build();
    ExoPlayer.Builder playerBuilder = new ExoPlayer.Builder(context, new TransformerRenderersFactory(context, muxerWrapper, removeAudio, removeVideo, transformationRequest, encoderFactory, decoderFactory, new FallbackListener(mediaItem, listeners, transformationRequest), debugViewProvider)).setMediaSourceFactory(mediaSourceFactory).setTrackSelector(trackSelector).setLoadControl(loadControl).setLooper(looper);
    if (clock != Clock.DEFAULT) {
        // Transformer.Builder#setClock is also @VisibleForTesting, so if we're using a non-default
        // clock we must be in a test context.
        @SuppressWarnings("VisibleForTests") ExoPlayer.Builder unusedForAnnotation = playerBuilder.setClock(clock);
    }
    player = playerBuilder.build();
    player.setMediaItem(mediaItem);
    player.addListener(new TransformerPlayerListener(mediaItem, muxerWrapper));
    player.prepare();
    progressState = PROGRESS_STATE_WAITING_FOR_AVAILABILITY;
}
Also used : DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) DefaultLoadControl(androidx.media3.exoplayer.DefaultLoadControl)

Example 5 with DefaultTrackSelector

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

the class DownloadHelper method runTrackSelection.

/**
 * Runs the track selection for a given period index with the current parameters. The selected
 * tracks will be added to {@link #trackSelectionsByPeriodAndRenderer}.
 */
@RequiresNonNull({ "trackGroupArrays", "trackSelectionsByPeriodAndRenderer", "mediaPreparer", "mediaPreparer.timeline" })
private TrackSelectorResult runTrackSelection(int periodIndex) {
    try {
        TrackSelectorResult trackSelectorResult = trackSelector.selectTracks(rendererCapabilities, trackGroupArrays[periodIndex], new MediaPeriodId(mediaPreparer.timeline.getUidOfPeriod(periodIndex)), mediaPreparer.timeline);
        for (int i = 0; i < trackSelectorResult.length; i++) {
            @Nullable ExoTrackSelection newSelection = trackSelectorResult.selections[i];
            if (newSelection == null) {
                continue;
            }
            List<ExoTrackSelection> existingSelectionList = trackSelectionsByPeriodAndRenderer[periodIndex][i];
            boolean mergedWithExistingSelection = false;
            for (int j = 0; j < existingSelectionList.size(); j++) {
                ExoTrackSelection existingSelection = existingSelectionList.get(j);
                if (existingSelection.getTrackGroup().equals(newSelection.getTrackGroup())) {
                    // Merge with existing selection.
                    scratchSet.clear();
                    for (int k = 0; k < existingSelection.length(); k++) {
                        scratchSet.put(existingSelection.getIndexInTrackGroup(k), 0);
                    }
                    for (int k = 0; k < newSelection.length(); k++) {
                        scratchSet.put(newSelection.getIndexInTrackGroup(k), 0);
                    }
                    int[] mergedTracks = new int[scratchSet.size()];
                    for (int k = 0; k < scratchSet.size(); k++) {
                        mergedTracks[k] = scratchSet.keyAt(k);
                    }
                    existingSelectionList.set(j, new DownloadTrackSelection(existingSelection.getTrackGroup(), mergedTracks));
                    mergedWithExistingSelection = true;
                    break;
                }
            }
            if (!mergedWithExistingSelection) {
                existingSelectionList.add(newSelection);
            }
        }
        return trackSelectorResult;
    } catch (ExoPlaybackException e) {
        // DefaultTrackSelector does not throw exceptions during track selection.
        throw new UnsupportedOperationException(e);
    }
}
Also used : TrackSelectorResult(androidx.media3.exoplayer.trackselection.TrackSelectorResult) ExoTrackSelection(androidx.media3.exoplayer.trackselection.ExoTrackSelection) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) ExoPlaybackException(androidx.media3.exoplayer.ExoPlaybackException) Nullable(androidx.annotation.Nullable) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Aggregations

DefaultTrackSelector (androidx.media3.exoplayer.trackselection.DefaultTrackSelector)5 ExoPlayer (androidx.media3.exoplayer.ExoPlayer)3 Surface (android.view.Surface)2 Nullable (androidx.annotation.Nullable)2 Test (org.junit.Test)2 Context (android.content.Context)1 Intent (android.content.Intent)1 SurfaceTexture (android.graphics.SurfaceTexture)1 PlaybackParameters (androidx.media3.common.PlaybackParameters)1 Player (androidx.media3.common.Player)1 Listener (androidx.media3.common.Player.Listener)1 TransferListener (androidx.media3.datasource.TransferListener)1 DefaultLoadControl (androidx.media3.exoplayer.DefaultLoadControl)1 ExoPlaybackException (androidx.media3.exoplayer.ExoPlaybackException)1 RenderersFactory (androidx.media3.exoplayer.RenderersFactory)1 AnalyticsListener (androidx.media3.exoplayer.analytics.AnalyticsListener)1 DrmSessionEventListener (androidx.media3.exoplayer.drm.DrmSessionEventListener)1 MediaPeriodId (androidx.media3.exoplayer.source.MediaSource.MediaPeriodId)1 MediaSourceEventListener (androidx.media3.exoplayer.source.MediaSourceEventListener)1 SelectionOverride (androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride)1