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?
4 Answers
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.
-
1This one didn't work for me until I set AutoPlay="True". Not sure why. I set name and autoplay in xaml, and set the source and sound.play() in code (as I'm using it to play multiple sounds), and it didn't work with AutoPlay at false, but it works perfect with it set to true. Thanks. Aug 30, 2011 at 13:39
-
1-1 because this way kills any other mp3 playing + also stops music if it was playing on the device. This will make your WP7 app not pass validation if you don't specifically get permission from the user to turn off its music. I just got turned down because of this. There really is no good way currently to use compressed audio on the WP7 platform. WP7 is surely quite weak when it comes to media support.– JonnyAug 31, 2011 at 17:04
Use Xna to play the sound. You can cross reference Xna from a Silverlight app though, for playing a sound file, you need to the following:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio
// ...
Stream stream = TitleContainer.OpenStream("sounds/bonk.wav");
SoundEffect effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
All the best for your application development!
-
5That's not true, Silverlight for Windows Phone 7 SUPPORTS audio playback. See msdn.microsoft.com/en-us/library/… Oct 26, 2010 at 7:57
-
2Beware - You should only use SoundEffect for short pieces of sound, not for background music. For that you should use a MediaElement Oct 26, 2010 at 8:16
-
What if i dont want the media player to be displayed? just want the background music– AqueelOct 26, 2010 at 8:51
-
1
-
@Aqueel: this is like the worst answer ever because it's an outright lie about silverlight. secondarily, if you're going to use xna, you need to hook the
.tick
event of a dispatch in order to actually have thesoundeffect
play. finally, if you use amediaelement
for sound but not video, it will never show up on your page as a visible item. why was this answer accepted? Apr 25, 2011 at 22:58
You'll want to use MediaElement. Here's a tutorial
http://create.msdn.com/en-US/education/quickstarts/Video_and_Audio
How about simply use a built-in behavior?
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<eim:PlaySoundAction Source="/Alarm1.wma" Volume="1"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
You need these two namespaces.
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:eim="clr-namespace:Microsoft.Expression.Interactivity.Media;assembly=Microsoft.Expression.Interactions"
-
Looks like a crear solution. Do you unos if it uses a Mediaelement? Jun 10, 2013 at 19:42