Archive for category Silverlight
I came across this odd bug in silverlight 2.0 Beta. For some reason some of my MP3 files were playing fine and some of my MP3 files were not playing at all. I was stumped for nearly an hour as to why this might be the case. The only difference between the files was the sample rate.
It seems that silverlight 2.0 doesn’t like playing MP3 files that used to be 22khz wav files. Changing the sample rate on my wavs to 44hz before using LAME to encode them to MP3s allowed me to playback the sound files as normal.
A little strange, guess I should report this bug to Microsoft – Maybe with broadband no one uses 22khz audio samples anymore.
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:
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