Class DefaultTrackSelector

  • Direct Known Subclasses:
    FakeTrackSelector

    public class DefaultTrackSelector
    extends MappingTrackSelector
    A default TrackSelector suitable for most use cases. Track selections are made according to configurable DefaultTrackSelector.Parameters, which can be set by calling setParameters(Parameters).

    Modifying parameters

    To modify only some aspects of the parameters currently used by a selector, it's possible to obtain a DefaultTrackSelector.ParametersBuilder initialized with the current DefaultTrackSelector.Parameters. The desired modifications can be made on the builder, and the resulting DefaultTrackSelector.Parameters can then be built and set on the selector. For example the following code modifies the parameters to restrict video track selections to SD, and to select a German audio track if there is one:
    
     // Build on the current parameters.
     Parameters currentParameters = trackSelector.getParameters();
     // Build the resulting parameters.
     Parameters newParameters = currentParameters
         .buildUpon()
         .setMaxVideoSizeSd()
         .setPreferredAudioLanguage("deu")
         .build();
     // Set the new parameters.
     trackSelector.setParameters(newParameters);
     
    Convenience methods and chaining allow this to be written more concisely as:
    
     trackSelector.setParameters(
         trackSelector
             .buildUponParameters()
             .setMaxVideoSizeSd()
             .setPreferredAudioLanguage("deu"));
     
    Selection DefaultTrackSelector.Parameters support many different options, some of which are described below.

    Selecting specific tracks

    Track selection overrides can be used to select specific tracks. To specify an override for a renderer, it's first necessary to obtain the tracks that have been mapped to it:
    
     MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
     TrackGroupArray rendererTrackGroups = mappedTrackInfo == null ? null
         : mappedTrackInfo.getTrackGroups(rendererIndex);
     
    If rendererTrackGroups is null then there aren't any currently mapped tracks, and so setting an override isn't possible. Note that a Player.Listener registered on the player can be used to determine when the current tracks (and therefore the mapping) changes. If rendererTrackGroups is non-null then an override can be set. The next step is to query the properties of the available tracks to determine the groupIndex and the trackIndices within the group it that should be selected. The override can then be specified using DefaultTrackSelector.ParametersBuilder.setSelectionOverride(int, com.google.android.exoplayer2.source.TrackGroupArray, com.google.android.exoplayer2.trackselection.DefaultTrackSelector.SelectionOverride):
    
     SelectionOverride selectionOverride = new SelectionOverride(groupIndex, trackIndices);
     trackSelector.setParameters(
         trackSelector
             .buildUponParameters()
             .setSelectionOverride(rendererIndex, rendererTrackGroups, selectionOverride));
     

    Constraint based track selection

    Whilst track selection overrides make it possible to select specific tracks, the recommended way of controlling which tracks are selected is by specifying constraints. For example consider the case of wanting to restrict video track selections to SD, and preferring German audio tracks. Track selection overrides could be used to select specific tracks meeting these criteria, however a simpler and more flexible approach is to specify these constraints directly:
    
     trackSelector.setParameters(
         trackSelector
             .buildUponParameters()
             .setMaxVideoSizeSd()
             .setPreferredAudioLanguage("deu"));
     
    There are several benefits to using constraint based track selection instead of specific track overrides:
    • You can specify constraints before knowing what tracks the media provides. This can simplify track selection code (e.g. you don't have to listen for changes in the available tracks before configuring the selector).
    • Constraints can be applied consistently across all periods in a complex piece of media, even if those periods contain different tracks. In contrast, a specific track override is only applied to periods whose tracks match those for which the override was set.

    Disabling renderers

    Renderers can be disabled using DefaultTrackSelector.ParametersBuilder.setRendererDisabled(int, boolean). Disabling a renderer differs from setting a null override because the renderer is disabled unconditionally, whereas a null override is applied only when the track groups available to the renderer match the TrackGroupArray for which it was specified.

    Tunneling

    Tunneled playback can be enabled in cases where the combination of renderers and selected tracks support it. Tunneled playback is enabled by passing an audio session ID to DefaultTrackSelector.ParametersBuilder.setTunnelingEnabled(boolean).