How to select audio tracks in different languages (HTML)

You can use the audioTracks property to switch between multiple audio tracks on a media file. For example, media files can contain multiple audio tracks in different languages. The audioTracks attribute is available on the Video and Audio elements.

You can search the list of audio tracks by iterating through the audioTracks property. When you find the desired track, select it by setting the enabled property to true. If the enabled track is changed, be sure to set the enabled property on the previous track to false.

The language of the track is represented by using a language code.

This example iterates through the audio tracks on a Video object and compares the language property to "en-gb". When the desired track is found, the enabled property is set to true.


function selectLang() {
    var myVideo = document.getElementById("videoTag1");
    
    if (myVideo.audioTracks.length > 1) {
        for (var i = 0; i < myVideo.audioTracks.length ; i++) {
            if (myVideo.audioTracks[i].language == "en-gb") {
                myVideo.audioTracks[i].enabled = true;
            }
            else {
                myVideo.audioTracks[i].enabled = false;
            }
        }
    }

    myVideo.play();
}


Related topics

Video
Audio
Media Playback Sample

 

 

Show:
© 2015 Microsoft