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

1import io.fastpix.media3.tracks.AudioTrackListener
2import io.fastpix.media3.tracks.AudioTrackUpdateReason
3
4player.addAudioTrackListener(object : AudioTrackListener {
5 override fun onAudioTracksLoaded(tracks: List<AudioTrack>, reason: AudioTrackUpdateReason) {
6 // Populate your UI (language menu, etc.)
7 }
8 override fun onAudioTracksChange(selectedTrack: AudioTrack) {
9 // Update selected state in your UI
10 }
11 override fun onAudioTrackSwitching(isSwitching: Boolean) {
12 // Optional: show/hide a "switching" indicator
13 }
14 override fun onAudioTracksLoadedFailed(error: AudioTrackError) {
15 // Handle invalid id, player not ready, selection failure, etc.
16 }
17})

Switch audio track by ID

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

1val tracks = player.getAudioTracks()
2val target = tracks.firstOrNull() ?: return
3player.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:

1player.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

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