Audio audioTracks Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Audio

Description

The audioTracks property returns a AudioTrackList object, which represents the available audio tracks for the audio.

Each available audio track is represented by an AudioTrack Object.

Return Value

Type Description
AudioTrackList Object Represents the available audio tracks for the audio.
AudioTrack Object Represents an audio track.

The AudioTrackList Object has the following method and properties:

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

The first available AudioTrack object is index 0

AudioTrack Object Properties:

  • id - get the id of the audio track
  • kind - get the type of the audio track (can be: "alternative", "description", "main", "translation", "commentary", or "" (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)

The following code shows how to Get the number of available audio tracks:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<audio id="myAudio" controls>
  <source src="your.ogg" type="audio/ogg">
  <source src="your.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>//from w  ww  . j ava  2s  .  co  m


<button onclick="myFunction()">get the number of available audio tracks</button>

<p id="demo"></p>

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

</body>
</html>

Related Tutorials