I finally got around to installing Visual Studio 2008 and the Silverlight 2.0 add-ons. My inital impressions were running high as I started a new project and mucked around with the XAML to create the obligatory Hello World App. I even managed to deploy my hello world app on my webserver - Yes, Apache can host Silverlight projects, pretty cool huh!
Everything was going fine until I tried to play some audio files. Since my project is heavily based around playing audio this would not do, not until I solved the problem of playing sound files that is!
First of all Silverlight doesn’t seem to support .wav files. But it will happily play .MP3s and .WMAs - Guess they figured .wavs were outdated and no one uses them anymore?
The class that plays audio files (and video files) is called a MediaElement, which takes a URI and points it towards a sound or video file. However, I found that the MediaElement would not start downloading the file until .play() was called - Which added a bit of delay the first time the file was played - I wanted to pre-load my sound files and here is the best way I found to do it:
Note: Silverlight 2.0 is still in Beta as I’m writing this, so things may change before final release.
Part 1 - Add the sound file as a resource
First of all add the file you want as a resource in your project. In Visual Studio 2008 right click on your Silverlight Project->Add->Existing Item
Then Select your file and under the Build Action property set it to ‘Resource’ - If you have a web project along with your silverlight project make sure you add the resource in your SL project and not to your web project.
If you do this when you build the project your file will be incorporated into your Silverlight application.
Part 2 - Play the Resource File
In order to play your newly added resource first include the line:
using System.Windows.Resources;
You will now be able to access streams to your resources. Use this code to create a MediaElement and get it to play an audio file.
MediaElement m = new MediaElement();
StreamResourceInfo sri = Application.getResourceStream(new Uri(”applicationName;component/myMp3.mp3″));
// Where applicationName = the name of your project and myMp3 is the name of the resource file you added
m.SetSource(sri.Stream); // Sets the MediaElement to point at the added resource
LayoutRoot.Children.Add(m); // Adds the MediaElement to the canvas, if you don’t do this then the sound seems to only play once (possible bug in silverlight?)
m.Play(); // Plays the sound
m.Position = System.TimeSpan.FromSeconds(0); // Reset the position of the sound so it is ready to play again

2 responses so far ↓
1 Kristie // Apr 24, 2008 at 8:08 pm
All I heard was blah blah geek geek blah blah
2 Andy // Jul 18, 2008 at 4:42 pm
Thanks for this post. Unfortunately, in Beta 2, I still can’t get this working. I’ve followed your steps exactly. sri.Stream looks good, and has a non-zero length, but after the call to SetSource, the m.Source property is “null.” I’ve also tried to set Source directly to no avail. This is way harder than it should be!!! Have you had any luck getting this to work with Beta 2?
Thanks,
Andy
Leave a Comment