Javascript DOM HTML Audio audioTracks length

Introduction

Get the number of available audio tracks:

var x = document.getElementById("myAudio").audioTracks.length;

Click the button to get the number of available audio tracks.

The audioTracks property is not supported in any major browsers.

View in separate window

<!DOCTYPE html>
<html>
<body>

<audio id="myAudio" controls>
  <source src="sound.ogg" type="audio/ogg">
  <source src="sound.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>/*from ww w . ja v  a2s .  co  m*/

<button onclick="myFunction()">Test</button>
<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("myAudio").audioTracks.length;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The audioTracks property returns a AudioTrackList object.

The AudioTrackList object stores the available audio tracks for the audio.

Each available audio track is represented by an AudioTrack Object.

AudioTrackList Object:

Value Description
audioTracks.length get the number of audio tracks available
audioTracks.getTrackById(id) get AudioTrack object by id
audioTracks[index] get AudioTrack object by index

AudioTrack Object Properties:

Property
Description
id
get the id of the audio track
kind







get the type of the audio track
Possible value:
"alternative"
"description"
"main"
"translation"
"commentary"
"" (empty string))
label
get the label of the audio track
language
get the language of the audio track
enabled
get or set if the track is active, true|false



PreviousNext

Related