@UnstableApi
public abstract class TrackSelector

Known direct subclasses
MappingTrackSelector

Base class for TrackSelectors that first establish a mapping between TrackGroups and Renderers, and then from that mapping create a ExoTrackSelection for each renderer.

Known indirect subclasses
DefaultTrackSelector

A default TrackSelector suitable for most use cases.

FakeTrackSelector

A fake MappingTrackSelector that returns FakeTrackSelections.


The component of an ExoPlayer responsible for selecting tracks to be consumed by each of the player's Renderers. The DefaultTrackSelector implementation should be suitable for most use cases.

Interactions with the player

The following interactions occur between the player and its track selector during playback.
  • When the player is created it will initialize the track selector by calling init.
  • When the player needs to make a track selection it will call selectTracks. This typically occurs at the start of playback, when the player starts to buffer a new period of the media being played, and when the track selector invalidates its previous selections.
  • The player may perform a track selection well in advance of the selected tracks becoming active, where active is defined to mean that the renderers are actually consuming media corresponding to the selection that was made. For example when playing media containing multiple periods, the track selection for a period is made when the player starts to buffer that period. Hence if the player's buffering policy is to maintain a 30 second buffer, the selection will occur approximately 30 seconds in advance of it becoming active. In fact the selection may never become active, for example if the user seeks to some other period of the media during the 30 second gap. The player indicates to the track selector when a selection it has previously made becomes active by calling onSelectionActivated.
  • If the track selector wishes to indicate to the player that selections it has previously made are invalid, it can do so by calling onTrackSelectionsInvalidated on the InvalidationListener that was passed to init. A track selector may wish to do this if its configuration has changed, for example if it now wishes to prefer audio tracks in a particular language. This will trigger the player to make new track selections. Note that the player will have to re-buffer in the case that the new track selection for the currently playing period differs from the one that was invalidated. Implementing subclasses can trigger invalidation by calling invalidate, which will call onTrackSelectionsInvalidated.
  • When the player is released, it will release the track selector by calling release.

Renderer configuration

The TrackSelectorResult returned by selectTracks contains not only TrackSelections for each renderer, but also RendererConfigurations defining configuration parameters that the renderers should apply when consuming the corresponding media. Whilst it may seem counter- intuitive for a track selector to also specify renderer configuration information, in practice the two are tightly bound together. It may only be possible to play a certain combination tracks if the renderers are configured in a particular way. Equally, it may only be possible to configure renderers in a particular way if certain tracks are selected. Hence it makes sense to determine the track selection and corresponding renderer configurations in a single step.

Threading model

All calls made by the player into the track selector are on the player's internal playback thread. The track selector may call onTrackSelectionsInvalidated from any thread.

Summary

Nested types

public interface TrackSelector.Factory

Factory for creating track selectors from contexts.

Notified when selections previously made by a TrackSelector are no longer valid.

Public constructors

Public methods

TrackSelectionParameters

Returns the current parameters for track selection.

@Nullable RendererCapabilities.Listener

Returns the RendererCapabilities.Listener that the concrete instance uses to listen to the renderer capabilities changes.

void
@CallSuper
init(
    TrackSelector.InvalidationListener listener,
    BandwidthMeter bandwidthMeter
)

Called by the player to initialize the selector.

boolean

Returns if this TrackSelector supports setParameters.

abstract void

Called by the player when a TrackSelectorResult previously generated by selectTracks is activated.

void

Called by the player to release the selector.

abstract TrackSelectorResult
selectTracks(
    RendererCapabilities[] rendererCapabilities,
    TrackGroupArray trackGroups,
    MediaSource.MediaPeriodId periodId,
    Timeline timeline
)

Called by the player to perform a track selection.

void

Called by the player to set the AudioAttributes that will be used for playback.

void

Called by the player to provide parameters for track selection.

Protected methods

final BandwidthMeter

Returns a bandwidth meter which can be used by track selections to select tracks.

final void

Calls onTrackSelectionsInvalidated to invalidate all previously generated track selections.

final void

Calls onRendererCapabilitiesChanged to invalidate all previously generated track selections because a renderer's capabilities have changed.

Protected fields

bandwidthMeter

protected @Nullable BandwidthMeter bandwidthMeter

Public constructors

TrackSelector

public TrackSelector()

Public methods

getParameters

public TrackSelectionParameters getParameters()

Returns the current parameters for track selection.

getRendererCapabilitiesListener

public @Nullable RendererCapabilities.Listener getRendererCapabilitiesListener()

Returns the RendererCapabilities.Listener that the concrete instance uses to listen to the renderer capabilities changes. May be null if the implementation does not listen to the renderer capabilities changes.

init

@CallSuper
public void init(
    TrackSelector.InvalidationListener listener,
    BandwidthMeter bandwidthMeter
)

Called by the player to initialize the selector.

Parameters
TrackSelector.InvalidationListener listener

An invalidation listener that the selector can call to indicate that selections it has previously made are no longer valid.

BandwidthMeter bandwidthMeter

A bandwidth meter which can be used by track selections to select tracks.

isSetParametersSupported

public boolean isSetParametersSupported()

Returns if this TrackSelector supports setParameters.

The same value is always returned for a given TrackSelector instance.

onSelectionActivated

public abstract void onSelectionActivated(@Nullable Object info)

Called by the player when a TrackSelectorResult previously generated by selectTracks is activated.

Parameters
@Nullable Object info

The value of info in the activated selection.

release

@CallSuper
public void release()

Called by the player to release the selector. The selector cannot be used until init is called again.

selectTracks

public abstract TrackSelectorResult selectTracks(
    RendererCapabilities[] rendererCapabilities,
    TrackGroupArray trackGroups,
    MediaSource.MediaPeriodId periodId,
    Timeline timeline
)

Called by the player to perform a track selection.

Parameters
RendererCapabilities[] rendererCapabilities

The RendererCapabilities of the renderers for which tracks are to be selected.

TrackGroupArray trackGroups

The available track groups.

MediaSource.MediaPeriodId periodId

The MediaPeriodId of the period for which tracks are to be selected.

Timeline timeline

The Timeline holding the period for which tracks are to be selected.

Returns
TrackSelectorResult

A TrackSelectorResult describing the track selections.

Throws
androidx.media3.exoplayer.ExoPlaybackException

If an error occurs selecting tracks.

setAudioAttributes

public void setAudioAttributes(AudioAttributes audioAttributes)

Called by the player to set the AudioAttributes that will be used for playback.

setParameters

public void setParameters(TrackSelectionParameters parameters)

Called by the player to provide parameters for track selection.

Only supported if isSetParametersSupported returns true.

Parameters
TrackSelectionParameters parameters

The parameters for track selection.

Protected methods

getBandwidthMeter

protected final BandwidthMeter getBandwidthMeter()

Returns a bandwidth meter which can be used by track selections to select tracks. Must only be called when the track selector is initialized.

invalidate

protected final void invalidate()

Calls onTrackSelectionsInvalidated to invalidate all previously generated track selections.

invalidateForRendererCapabilitiesChange

protected final void invalidateForRendererCapabilitiesChange(Renderer renderer)

Calls onRendererCapabilitiesChanged to invalidate all previously generated track selections because a renderer's capabilities have changed.

Parameters
Renderer renderer

The renderer whose capabilities changed.