How to Select Audio Tracks in Various Languages in Windows Store Apps

Introduction

In this article we are explaining how to select an audio track in a different language in Windows Store apps using C# and XAML. The Windows Store app platform allows app to select an audio track with a different language when playing media that has a different language.

In Windows Store apps we use "MediaElementName.AudioStreamIndex" and "MediaElementName.GetAudioStreamLanguage" to switch among multiple language audio tracks on a media file. Multiple audio tracks can also include additional tracks in the same language. You can also search for the number of a track in a specific language by looping through each audio track and checking its language using "AudioStreamCount". Use the "GetAudioStreamLanguage" method to get the language of the track. The language of the track is identified by a language code. When you find the track with the desired language, set the "AudioStreamIndex" to the index of the track.

Before using "how to select audio track in a different language in Windows Store apps" you must must be familiar with "how to play a media file in Windows Store apps".

Step 1

Open Visual Studio 2012 and start a new Windows Store project or open an existing MediaElement project for which you want to add the option for multitple tracks.

Step 2

Go to Solution Explorer and double-click on the "MainPage.xaml" page to open it.

Solution-Explorer-Windows-Store-Apps.jpg

Step 3

You can use this code to set a specific language of a media file if the media file supports the language. In this example we are using the Hindi ("hi") language. You can use whatever language codes that are relevant to you.

for (int index = 0; index < VideoElement.AudioStreamCount; index++)
{if (VideoElement.GetAudioStreamLanguage(index) == "hi"){VideoElement.AudioStreamIndex = index;}
}

Step 4

If you want to search and select a number of a track of the media file then use this code. Your "MainPage,xaml" page is as the following:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

        <MediaElement Source="Video.mp4" x:Name="VideoElement" Height="200" Width="400"/>

        <ComboBox HorizontalAlignment="Left" x:Name="SelectTrack" VerticalAlignment="Top" Width="120" SelectionChanged="SelectTrack_SelectionChanged" Margin="521,225,0,0"/>

    </Grid>

Step 5

And the "MainPage.xaml.cs" page is as the following:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

using Windows.ApplicationModel.DataTransfer;

 

namespace MultiTrack

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        } 

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {           

        }

        private void SelectTrack_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            VideoElement.AudioStreamIndex = SelectTrack.SelectedIndex;

        }

        private void PopulateAudioTracks(MediaElement media, ComboBox audioSelection)

        {

            if (media.AudioStreamCount > 0)

            {

                for (int index = 0; index < media.AudioStreamCount; index++)

                {

                    ComboBoxItem track = new ComboBoxItem();

                    track.Content = media.GetAudioStreamLanguage(index);

                    audioSelection.Items.Add(track);

                }

            }

        }

    }

}


Similar Articles