I recently completed my first Silverlight animation and I ended up stumbling with playing and looping an MP3 file for the animations background music. My first instinct was to add the MP3 file to the Solution but quickly found that Silverlight doesn't handle media file as an embed asset within its compiled XAP file but as an outside asset.
To start, Silverlight requires you to place all media files inside your ClientBin. Here is a look at my projects structure.
/Build/ClientBin/Splash.xap
/Build/ClientBin/English.xap
/Build/ClientBin/French.xap
/Build/ClientBin/Music.mp3
/Build/Index.html
/Build/English.html
/Build/French.html
We begin by adding the MediaElement node. Use the x:Name attribute to define its unique name and Source to define our MP3's location; the path is based on our folder structure and if I had my MP3 file within a folder called Media, /Build/ClientBin/Media/Music.mp3, the path would be Source="Media/Sound.mp3".
-
<MediaElement x:Name="SoundTrack" Source="Music.mp3" />
By default the current XAML will only load our MP3 file with the XAP file so that it is accessible when your Silverlight application or animation starts. In order for us to hear our MP3, we can use the AutoPlay attribute to begin playing once the XAP file has finished loading.
-
<MediaElement x:Name="SoundTrack" Source="Music.mp3" AutoPlay="True" />
At this point if we build and run our project, we will hear our music playing in the background. If you're following this article with your own files, wait to the end of your track. You will notice that, by default all media files, play once.
Unfortunately XAML does not provide us with an attribute like Loop="True", therefore to accomplish a loop, we must use some C# code.
To start, lets add the MediaEnded attribute to our MediaElement node; if you are using Visual Studio 2008 it will auto-compete a value of SoundTrack_MediaEnded and create the method, by the same name, in your C# code. If you are using Expressions Blend 2.5, who does not provide IntelliSense, you will need enter the value yourself and also go into you C# code and add the method SoundTrack_MediaEnded.
XAML
-
<MediaElement x:Name="SoundTrack" Source="Music.mp3" AutoPlay="True" MediaEnded="SoundTrack_MediaEnded" />
C#
-
private void SoundTrack_MediaEnded (object sender, RoutedEventArgs e)
-
{
-
-
}
Switch to your C# code and find SoundTrack_MediaEnded. As it stands right now, our XAML knows to call this function when our SoundTrack ends. In order to set a loop for our background music, we will need ensure we seek back to 0 and start its playback again.
-
private void SoundTrack_MediaEnded (object sender, RoutedEventArgs e)
-
{
-
SoundTrack.Stop();
-
SoundTrack.Play();
-
}
If you build and run your project, you should have your audio file playing and looping.
Make sure you check the documentation for a list all available attributes but one that you may need to use right away is the Volume attribute; uses a value from 0 to 1.
-
<MediaElement x:Name="SoundTrack" Source="Music.mp3" AutoPlay="True" MediaEnded="SoundTrack_MediaEnded" Volume=".8" />

Recent Comments