Skip to main content
Version: 11.8.0

How to programmatically detect audio track changes on iOS

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 iOS SDK applies to all iOS-based platforms, including iPadOS and tvOS.

The iOS SDK exposes the AudioTrack API through player.audioTracks. This audioTracks property is a MediaTrackList. This MediaTrackList dispatches the events from the AudioTrackListEventTypes. The AudioTrackListEventTypes contains the CHANGE event, as well as the ADD_TRACK and REMOVE_TRACK event.

The code below allows you to detect audio track changes.

player?.audioTracks.addEventListener(type: AudioTrackListEventTypes.CHANGE, listener: { event in
let track: AudioTrack = event.track as! AudioTrack
print(track.label, track.language, track.enabled)
})

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: