views:

97

answers:

3

Hi I am working on Windows 7 based application development in Silverlight. I have not been able to find a way to play a an audio file in windows 7 phone programmatically. I have been googling it since past few days but i could not get any solution of that. There is a class SoundPlayer in C# but i guess its not available in Windows 7 Phone. Can anyone please help?

Best Regards

+1  A: 

Silverlight for Windows Phone 7 doesn't support audio playback, you have to use Xna to play sound. You can cross reference Xna from a Silverlight app though, for playing a sound file, you need to the following:

  1. include these references in your silverlight app: Microsoft.Xna.Framework and Microsoft.Xna.Framework.Audio
  2. Create a SoundEffect object from your audio file like SoundEffect sound = SoundEffect.FromStream(TitleContainer.OpenStream([sound file name])); <- replace sound file name with appropriate path of your sound file
  3. To play the sound, execute the following code FrameworkDispatcher.Update(); sound.Play();

All the best for your application development!

vamana
Wow that was great. Thanks a lot vamana :)
Aqueel
That's not true, Silverlight for Windows Phone 7 SUPPORTS audio playback. See http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement(VS.95).aspx
Olivier PAYEN
Beware - You should only use SoundEffect for short pieces of sound, not for background music. For that you should use a MediaElement
Matt Lacey
What if i dont want the media player to be displayed? just want the background music
Aqueel
+2  A: 

You can place a MediaElement in your XAML view:

<MediaElement 
    x:Name="sound" 
    Source="sound.wma" 
    AutoPlay="False" />

then in the code-behind:

sound.Play();

Supported formats are MP3 and WMA.

Olivier PAYEN