How to select audio tracks in different languages (HTML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

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();
}

Video

Audio

Media Playback Sample