Skip to navigation

Switch audio tracks

Learn how to discover and switch between audio tracks in the FastPix Android Player.

FastPixPlayer can discover and switch audio tracks, commonly available on HLS streams with multiple audio renditions.

AudioTrack properties

Each AudioTrack includes the following properties (useful for building a UI):

  • id: pass this to setAudioTrack(id) to switch tracks.
  • languageCode / languageName / label: display in your language menu.
  • isSelected, isPlayable, isDefault: for UI state.
  • Optional diagnostics: role, channels, codec, bitrate, groupId.

Listen for audio track updates

import io.fastpix.media3.tracks.AudioTrackListener
import io.fastpix.media3.tracks.AudioTrackUpdateReason
player.addAudioTrackListener(object : AudioTrackListener {
override fun onAudioTracksLoaded(tracks: List<AudioTrack>, reason: AudioTrackUpdateReason) {
// reason is INITIAL, MEDIA_CHANGED, or TRACKS_UPDATED
// Populate your UI (language menu, etc.)
}
override fun onAudioTracksChange(selectedTrack: AudioTrack) {
// Update selected state in your UI
}
override fun onAudioTrackSwitching(isSwitching: Boolean) {
// Optional: show/hide a "switching" indicator
}
override fun onAudioTracksLoadedFailed(error: AudioTrackError) {
// Handle invalid id, player not ready, selection failure, etc.
}
})

Switch audio track by ID

Note: If a seek is in progress, the SDK defers switching until the seek completes.

val tracks = player.getAudioTracks()
val current = player.getCurrentAudioTrack() // currently selected track, null if only one track or none
val target = tracks.firstOrNull() ?: return
player.setAudioTrack(target.id)

Set a default audio language

Set a preferred audio language that applies automatically when tracks become available. This never overrides a manual selection:

player.setDefaultAudioTrack(languageName = "English")

Use BCP-47 / ISO language names like "English", "Hindi", "French". If the preferred language doesn’t exist in the stream, the player keeps its normal selection.

Build a language menu

val tracks = player.getAudioTracks()
val items = tracks.filter { it.isPlayable }.map { t ->
val title = t.languageName ?: t.label ?: t.languageCode ?: "Unknown"
title to t.id
}
// Present items in a dialog; on click: player.setAudioTrack(id)

Remove the audio track listener

Keep a reference to your AudioTrackListener and remove it when it is no longer needed:

player.removeAudioTrackListener(listener)

Frequently asked questions

How do I change the audio language on the FastPix Android player?

Call player.setAudioTrack(id) with an id from getAudioTracks() to switch immediately, or call player.setDefaultAudioTrack(languageName = "Hindi") so a preferred language is applied automatically when tracks load. Switching does not restart playback: position and state are preserved.

How do I switch the audio track in an HLS stream?

Audio track switching works on media that provides multiple audio renditions, which HLS streams commonly do. Read the tracks with player.getAudioTracks(), then call player.setAudioTrack(track.id). If a seek is in progress, the SDK defers the switch until the seek completes.

How do I set a default audio language?

Call player.setDefaultAudioTrack(languageName = "English") with a BCP-47 / ISO language name. The preference is applied automatically when tracks become available, such as after loading new media, and it never overrides a manual selection. If the language is not in the stream, the player keeps its normal selection.

What information does the AudioTrack model provide?

Each AudioTrack exposes id (pass it to setAudioTrack), plus languageCode, languageName, and label for your menu, and isSelected, isPlayable, and isDefault for UI state. Optional diagnostics include role, channels, codec, bitrate, and groupId.


What’s next