Tuesday 24 April 2012

Silverlight Audio Support

Adding audio to a Silverlight application is fairly easy, because you can throw in just about any MP3 file. Using a video file is more work. Not only must you make sure you’re using one of the supported WMV formats, but you also need to carefully consider the quality you need and the bandwidth your visitors can support.

For audio, Silverlight supports the following :

• Windows Media Audio (WMA) [versions 7, 8, and 9]
• MP3

All the audio and video functionality is built into a single class: MediaElement. A simple MediaElement tag is all you need to play a sound. For example, add this markup to your user interface :

<MediaElement Source="test.mp3"></MediaElement>

The startup behavior of the MediaElement is determined by its AutoPlay property. If this property is set to false, the audio file is loaded, but your code takes responsibility for starting the playback at the right time :

<MediaElement x:Name="media" Source="test.mp3" AutoPlay="False"></MediaElement>

When using this approach, you must make sure to give the MediaElement a name so that you can interact with it in code. Generally, interaction consists of calling the Play(), Pause(), and Stop() methods.
We can change the Position property to move through the audio.
Here’s a simple event handler that seeks to the beginning of the current audio file and then starts playback : 

private void cmdPlay_Click(object sender, RoutedEventArgs e)
{
media.Position = TimeSpan.Zero;
media.Play();
}

The two check boxes on the page are the last ingredient in this media player and one of the simplest details. The Mute check box sets the corresponding IsMuted property of the MediaElement :
private void chkMute_Click(object sender, RoutedEventArgs e)
{
media.IsMuted = (bool)chkMute.IsChecked;
}

Handling errors :
MediaElement doesn’t throw an exception if it can’t find or load a file. Instead, it’s up to you to handle the MediaFailed event. Fortunately, this task is easy. First, tweak your MediaElement tag as shown here :
<MediaElement ... MediaFailed="media_MediaFailed"></MediaElement>
Then, in the event handler, you can use the
ExceptionRoutedEventArgs.ErrorException property to get an exception object that describes
the problem. Here’s an example that displays the appropriate error message:
private void media_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
lblErrorText.Text = e.ErrorException.Message;
}

The MediaElement exposes a number of properties that allow you to control your playback.
Volume : Sets the volume as a number from 0 (completely muted) to 1 (full volume). The default value is 0.5.set IsMuted to true for temporarily muting.

Balance : Sets the balance between the left and right speaker as a number from -1 (left speaker only), 1(right speaker only) and 0 by default.

Position : Provides a TimeSpan object that indicates the current location in the media file.

<Slider Grid.Column="1" x:Name="sliderVolume" Minimum="0" Maximum="1" Value="0.5"
ValueChanged="sliderVolume_ValueChanged" ></Slider>

<Slider Grid.Row="1" Grid.Column="1" x:Name="sliderBalance" Minimum="-1" Maximum="1"
ValueChanged="sliderBalance_ValueChanged"></Slider>

When the user drags the thumb in the slider, the change is applied to the MediaElement :
private void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
media.Volume = sliderVolume.Value;
}

private void sliderBalance_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
media.Balance = sliderBalance.Value;
}

The Mute check box sets the corresponding IsMuted property of the MediaElement :
private void chkMute_Click(object sender, RoutedEventArgs e)
{
media.IsMuted = (bool)chkMute.IsChecked;
}

No comments:

Post a Comment