Skip to main content

Track Selection

Track types

Each playback can have three types of tracks - AUDIO , VIDEO and TEXT. These different track types are denoted using TrackVariantInfo.Type.

//VIDEO Track type
val videoTrackType = TrackVariantInfo.Type.VIDEO
//AUDIO Track Type
val audioTrackType = TrackVariantInfo.Type.AUDIO
//TEXT Track type
val textTrackType = TrackVariantInfo.Type.TEXT

Tracks

Each elementary track of each type related to the playback is represented as a TrackVariantInfo object. Each track is differentiated using bitrate, language, mimetype, etc. The metadata of the track can be obtained using the various properties of this object.

Property NameTypeDescription
idStringThe unique identifier of the track.
typeTypeThe type of the track.
languageCodeStringEither a valid 2-letter ISO 639-1 Language code or a valid 3-letter ISO 639-2 Language code.
mimeTypeStringMime Type of the option (video/mp4, video/webm, audio/mp4, audio/eac3, text/ttml, text/webvtt etc.)
bitRateIntThe bitrate of the track (-1 if not applicable)
channelCountIntThe number of channels of the track (-1 if not applicable)
codecsStringThe codecs used by the track (mp4a.40.2, avc1.64000D, etc.)
sampleRateIntThe sample rate of the track (-1 if not applicable)

Available Tracks

All the available tracks of each type of track can be obtained using the availableTrackVariants API on the player. This API returns an Array of TrackVariantInfo objects.

// get all video tracks
val availableVideoTracks = player.availableTrackVariants(TrackVariantInfo.Type.VIDEO)

//get all audio tracks
val availableAudioTracks = player.availableTrackVariants(TrackVariantInfo.Type.AUDIO)

//get all subtitle tracks
val availableTextTracks = player.availableTrackVariants(TrackVariantInfo.Type.TEXT)

Get active Track

The currently active track in each type can be obtained from the Player using the activeTrackVariant API. This API returns the active track of any given Type as a TrackVariantInfo object and TrackVariantInfo.UNKNOWN if active variant is not known.

val activeSubtitleTrack = player.getActiveTrackVariant(TrackVariantInfo.Type.TEXT)

val activeAudioTrack = player.getActiveTrackVariant(TrackVariantInfo.Type.AUDIO)

val activeVideoTrack = player.getActiveTrackVariant(TrackVariantInfo.Type.VIDEO)

Set preferred Track

Any particular track can be enabled using setPreferredTrackVariant API on the player. This API takes the preferred track parameter as a TrackVariantInfo object.
Pass TrackVariantInfo.NONE to clear all existing selection preferences.

//enable first subtitle track
player.setPreferredTrackVariant(TrackVariantInfo.Type.TEXT, availableTextTracks[0])

//disable subtitles
player.setPreferredTrackVariant(TrackVariantInfo.Type.TEXT, TrackVariantInfo.NONE)

//enable first audio track
player.setPreferredTrackVariant(TrackVariantInfo.Type.AUDIO, availableAudioTracks[0])

//enable first video track
player.setPreferredTrackVariant(TrackVariantInfo.Type.VIDEO, availableVideoTracks[0])

Set maximum / minimum bitrates

Configures the Player's variant adaptation logic with the passed maximum and minimum bitrate constraints.

const val MAX_BITRATE_BPS = 780_000 // use preferred value here
const val MIN_BITRATE_BPS = 140_000 // use preferred value here

player.setPreferredBitrateThresholds(MAX_BITRATE_BPS, MIN_BITRATE_BPS)

//pass (Integer.MAX_VALUE,0) to reset any capping
player.setPreferredBitrateThresholds(Integer.MAX_VALUE, 0)