Skip to main content
Version: 11.7.0

How to programmatically detect audio track changes on Android

This article describes how you can use the AudioTrack API to detect audio track changes. An audio track "change" is triggered by enabling (or disabling) an audio track.

Implementing this functionality is a common use-case for developers who want to build their own UI, and annotate the audio track that is currently active.

Usage

The implementation of the Android SDK applies to all Android-based platforms, including Android TV and Fire TV.

The Android SDK exposes the AudioTrack API through player.getAudioTracks(). This getAudioTracks() method returns a MediaTrackList that inherits from the TrackList. This TrackList dispatches the events from the AudioTrackListEventTypes. The AudioTrackListEventTypes contains the TRACKLISTCHANGE event, as well as the ADDTRACK and REMOVETRACK event.

The code below allows you to detect audio track changes.

player.audioTracks.addEventListener(AudioTrackListEventTypes.TRACKLISTCHANGE) { event ->
val track = event.track
println("${track.label}, ${track.language}, ${track.isEnabled}")
}

The properties of a media track (e.g. enabled, language) are described in the MediaTrack and Track API references.

Are you reading this article because you are interested in audio tracks? Continue reading below.

Are you reading this article because you are implementing a custom UI? Then you will find the following articles interesting: