Blog
FacebookYoutubeTwitterFlickr

Play sound in Silverlight using MediaElement

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

Source:  http://silverlighttips.com/?tag=/mediaelement

 

Comments:

57 Votes
1 Comment
  1. thanks, nice article :)
    Ahmad Khalid
    Monday, 05 September 2011
Leave a Comment