How to programmatically detect audio track changes
This article describes how you can use the THEOplayer API to detect audio track quality changes. An audio track "change" is triggered by enabling (or disabling) an audio track.
The AudioTrack API provides this functionality.
More specifically, as a developer, you'll subscribe to the change
event in the AudioTrack API.
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.
SDKs
THEOplayer allows you to track audio track changes on the following SDKs.
Web SDK | Android SDK | iOS SDK | tvOS SDK | Android TV SDK | Chromecast SDK | Roku SDK |
---|---|---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes | Yes |
Implementation
The AudioTrack API is available across all of our base SDKs. As described in the introduction, to detect text track changes, you want to detect the change
event in the AudioTrack
API.
Web SDK
The implementation of the Web SDK applies to all web-based platforms, including Tizen and webOS.
The Web SDK exposes the AudioTrack API through player.audioTracks
.
This audioTracks
property is a MediaTrackList
that inherits from the TrackList
.
This TrackList
dispatches the events from the TrackListEventMap
.
This TrackListEventMap
contains the change
event, as well as the addtrack
and removetrack
event.
The code below allows you to detect text track changes.
player.audioTracks.addEventListener('change', function (event) {
const track = event.track;
console.log(track, track.label, track.language, track.enabled);
});
The properties of a media track
(e.g. enabled
, language
) are described in the MediaTrack
API reference.
Android SDK
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.getAudioTracks().addEventListener(AudioTrackListEventTypes.TRACKLISTCHANGE, trackListChangeEvent -> {
MediaTrack track = trackListChangeEvent.getTrack();
System.out.println(track.getLabel() + ", " + track.getLanguage() + ", " + track.isEnabled());
});
The properties of a media track
(e.g. enabled
, language
) are described in the MediaTrack
and Track
API references.
iOS/tvOS SDK and Legacy iOS/tvOS SDK (4.12.x)
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.
Roku SDK
This subsection is in maintenance. Reach out to our team if you need help.
Related articles
Are you reading this article because you're interested in audio tracks? Continue reading below.
Are you reading this article because you're implementing a custom UI? Then you'll find the following articles interesting: