Condividi tramite


How to select audio tracks in different languages (XAML)

[ 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 ]

Use MediaElement.AudioStreamIndex and MediaElement.GetAudioStreamLanguage to change the audio to a different language track on a video. Videos can also contain multiple audio tracks in the same language, such as director commentaries on films. This example shows how to specifically switch between different languages, but this code can easily be modified for other purposes.

Roadmap: How does this topic relate to others? See:

Prerequisites

This topic assumes that you know how to create a basic Windows Runtime app using C++, C#, or Visual Basic. For help creating your first app, see Create your first Windows Store app using C# or Visual Basic.

This topic assumes you are familiar with the MediaElement class. For an introduction on using the MediaElement class, see the Quickstart: video and audio.

Instructions

Step 1: Get the audio tracks

To search for a track in a specific language, start by iterating through each audio track on the video. Use AudioStreamCount as the max value for a for loop.

Step 2: Get the language of the audio track

Use the GetAudioStreamLanguage method to get the language of the track. The language of the track is identified by a language code, such as "en" for English or "ja" for Japanese.

Step 3: Set the active audio track

When you find the track with the desired language, set the AudioStreamIndex to the index of the track. Setting AudioStreamIndex to null will select the default audio track which is defined by the content.

Step 4: Code example

Here is some code that attempts to set the audio track to the specified language. It iterates through the audio tracks on a MediaElement object and uses GetAudioStreamLanguage to get the language of each track. If the desired language track exists, the AudioStreamIndex is set to the index of that track.

/// <summary>
/// Attemps to set the audio track of a video to a specific language
/// </summary>
/// <param name="lcid">The id of the language. For example, "en" or "ja"</param>
/// <returns>true if the track was set; otherwise, false.</returns>
private bool SetAudioLanguage(string lcid, MediaElement media)
{
    bool wasLanguageSet = false;

    for (int index = 0; index < media.AudioStreamCount; index++)
    {
        if (media.GetAudioStreamLanguage(index) == lcid)
        {
            media.AudioStreamIndex = index;
            wasLanguageSet = true;
        }
    }

    return wasLanguageSet;
}

Roadmaps

Roadmap for Windows Runtime apps using C# and Visual Basic

Roadmap for Windows Runtime apps using C++

Designing UX for apps

Adding multimedia

Samples

XAML media playback sample

Media playback, start to finish

Tasks

Quickstart: video and audio

Quickstart: create a media player application

Reference

MediaElement

AudioStreamIndex

GetAudioStreamLanguage

AudioStreamCount

Other resources

Supported audio and video formats

Optimize media resources