Silverlight Tips provides simple and useful tutorials and tips with real life examples, live demos and sample codes to download.
About authors:
Damon Serji
Damon Serji,
Silverlight developer working at Metia in London.
Gavin Wignall
Gavin Wignall,
Interactive Design Lead, working in design for over 10 years, the last 3 being in Silverlight.
Allan Muller
Allan Muller,
Developer, working on various types of Silverlight and WCF projects.

If you want to play music or a short sound clip in Silverlight here is what you need to do:

1. Your sound file
First, make sure you sound file is MP3 or WMA format as other formats are not supported.

Save your file to ClientBin folder (where your xap file gets coppied when buiding the project)

If you dont want to store your file to the ClientBin folder do this: from Visual Studio right click on the sound file and select Properties. Now in Properties window change Build Action to Resource. This ensures the file gets copied to the ClientBin folder during execution.

2. Add a MediaElement in your Xaml file


<MediaElement x:Name="SoundClip" Source="boing.mp3" Volume="1" AutoPlay="False"></MediaElement>

set the Source to be the name of your file and AutoPlay to False so it can be played at click of a button.

Also add a button to play the sound when clicked:


<Button x:Name="PlaySound" Content="Play Sound" />

3. play the sound
in your code behind add this to the Click event of your button:

SoundClip.Play();

If you have any questions please visit Silverlight Forums here: http://silverlightforums.com/showthread.php?p=2080#post2080

Posted by Allan Muller on 10. December 2009 13:16 under: Basic
 with 1 Comments

Comments

You may also want to look at this:
stackoverflow.com/.../using-silverlight-2-for-short-audio-caching

Posted 12/11/2009 7:08:39 AM

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview