# Accessing Alternate Audio Tracks in Silverlight Media

ID: SL1

Technology: silverlight

Type: Technique

## When to Use

- Microsoft Silverlight, versions 3 and greater
- Silverlight managed programming model and Silverlight XAML

## Description

The objective of this technique is to show how to access an alternate audio channel in a prepared media file that is played in a Silverlight `MediaElement`.

Silverlight supports media file formats that contains additional audio channels in synchronization, beyond the two tracks for stereo audio that are used by typical media player defaults. Silverlight provides a dedicated `AudioStreamIndex` API on `MediaElement`, so that the Silverlight application author can use Silverlight programming techniques to select which audio channel to play for the user. Silverlight control authors might label a UI control with text such as "Activate this button to listen to an audio-only version of the media presentation" so that the purpose of the media element control interface is clear to the user. That way the same media control can be used to present the media either as audio-video or as audio-only with alternate track depending on user preference at run time.

The media formats that are supported by Silverlight are documented on MSDN.

#### Media encoding

The process of encoding the media with additional audio channels is not described in this technique because configuring and encoding audio channels for media formats is a technique for any usage of media in a computer application, not just a Silverlight-specific technique or a Web technology technique. For more information on one possible procedure for encoding the media in WMV format, see Microsoft Expression Encoder Overview. Often, Silverlight authors will receive the media from a third party, such as a video production facility, and are not directly involved with the encoding process. Silverlight authors should verify that the media they are using has alternate audio tracks encoded in it. If such tracks exist, Silverlight authors will need a track listing from the media producer to know which of the audio tracks is intended as the alternate audio. Other tracks might exist in the encoded media that provide language translations of the default audio, or that serve other purposes.

## Examples

### Changing AudioStreamIndex

This example has a UI definition in XAML and interaction logic in C#. In addition to the typical Play/Pause/Stop controls, this interface includes a Play Full-Description Audio button. Activating the button invokes a function that swaps the audio channels and plays an alternative synchronized audio channel that contains a composite full-description audio track.

The following is the basic UI in XAML. This example is deliberately simple and does not include `AutomationProperties`. Audio streams are identified by an index in a collection.

          <Grid x:Name="LayoutRoot" Background="White">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="20" />
            </Grid.RowDefinitions>
            <MediaElement x:Name="media" Source="/combined.wmv"
               Width="300" Height="300"
               Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3"
               AutoPlay="false"
            />
            <Button Click="StopMedia"
         Grid.Column="0" Grid.Row="1" Content="Stop" />
            <Button Click="PauseMedia"
         Grid.Column="1" Grid.Row="1" Content="Pause" />
            <Button Click="PlayMedia"
         Grid.Column="2" Grid.Row="1" Content="Play" />
            <Button Name="AltAudioBtn" Grid.Row="2" HorizontalAlignment="Left" Grid.ColumnSpan="2"
            Click="AltAudioBtn_Click">Play Full-Description Audio</Button>
        </Grid>

The following is the C# logic.

            private void AltAudioBtn_Click(object sender, RoutedEventArgs e)
            {
                if (media.AudioStreamCount > 1)
                {
                    if (media.AudioStreamIndex == 1)
                    {
                        media.AudioStreamIndex = 0;
                        (sender as Button).Content = "Play full-description audio";
                    } else {
                        media.AudioStreamIndex = 1;
                       (sender as Button).Content = "Play default audio";
                    }
                }
                else
                {
                    (sender as Control).IsEnabled = false;
                }
            }
            private void StopMedia(object sender, RoutedEventArgs e)
            {
                media.Stop();
            }
            private void PauseMedia(object sender, RoutedEventArgs e)
            {
                media.Pause();
            }
            private void PlayMedia(object sender, RoutedEventArgs e)
            {
                media.Play();
            }

This example is shown in operation in the working example of Alternative Audio Channel. If using the test file, the test contains test audio tones rather than actual audio description, but the pitch of the tones is indicative of which of the channels is selected and played.

## Tests

### Procedure

1. Open the HTML page for a Silverlight application, where that application plays media and the media is expected to support an alternate audio track for the video.
2. Verify that the application user interface presents a control that enables the user to cause the media to play with an alternate audio track.
3. Activate that control. Verify that the audio portion of the media player output as played through the computer's audio system is now playing the alternate audio track.

### Expected Results

#2 and #3 are true.

## Resources

- Audio and Video Overview
- MediaElement Class
- AudioStreamIndex Property