[WP7] Sound effect in a Silverlight Windows Phone 7 application
Windows Phone August 12th, 2010A coworker and I are currently working on a simple Silverlight game for the Windows Phone 7 platform. In order to give some feedback to our end-user, we decided to add sound effects. Here is a very short post about how we did that.
The first thing is to reference the Microsoft.Xna.Framework assembly in your project. This assembly is needed to access the low-level sound component of XNA right from your Silverlight application. You also need to have your sound effect in a WAVE format file.
Then we created a simple action (from the Blend Behavior toolkit) which is a TargetedTriggerAction:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class SoundEffectAction : TargetedTriggerAction<FrameworkElement> { public string Source { get { return (string)GetValue(SourceProperty); } set { SetValue(SourceProperty, value); } } public static readonly DependencyProperty SourceProperty = DependencyProperty.Register( "Source", typeof(string), typeof(SoundEffectAction), new PropertyMetadata(string.Empty)); protected override void Invoke(object parameter) { if(!string.IsNullOrEmpty(this.Source) { var stream = TitleContainer.OpenStream(this.Source); if (stream != null) { var effect = SoundEffect.FromStream(stream); FrameworkDispatcher.Update(); effect.Play(); } } } } |
Using this action, we’re able to wire sound effect right in Blend which produces the following XAML code:
1 2 3 4 5 6 7 8 | <Grid Margin="2"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonUp"> <Actions:SoundEffectAction Source="Resources/Sounds/Click.wav" /> </i:EventTrigger> </i:Interaction.Triggers> <!-- rest of the xaml... --> </Grid> |
Simple, isn’t it ?



September 2nd, 2010 at 11:12 am
Hello Jérémy.
Great code !
I have just a question : why do you use a TargetedTriggerAction, and not just a TriggerAction ?
September 2nd, 2010 at 11:21 am
Fred,
Actually I think TriggerAction would have done the trick too. Maybe the ability to target this action to a different ui element is not needed here…
September 14th, 2010 at 7:25 am
Hello Jeremy,
coworker is more funny that collaborateur
are touch screen triggers and events available, like fingerOneUp, fingerTwo…?
have a good day.
September 15th, 2010 at 11:26 am
Frederic,
You can check out this blog post for a collection of gesture-related behaviors: http://blogs.claritycon.com/blogs/windows_phone_7/archive/2010/07/19/wp7-gesture-recognizer-and-behavior-triggers.aspx
November 24th, 2011 at 10:17 am
hi, i have 2 questions:
1. how do i access the invoke method from my main page class, since it is protected and in the SoundEffectAction class? i want to use code vs. XAML.
2. shouldn’t it be: SoundEffect effect = SoundEffect.FromStream(stream);
thanks.
November 24th, 2011 at 2:49 pm
Ronen,
1/ you cannot invoke this method from your main page because it’s not the point actually. The goal is to attach the ability to play sound to an UI element.
2/ You can specify the type explicitly (like SoundEffect effect) or let the compiler infer the real type (using var effect). It does not change anything at runtime.
November 24th, 2011 at 6:43 pm
thanks. for your reply.
1. got it. but VS2010 with SL4 didn’t like …so i just created another method PlaySound() which is exactly as your Invoke but not protected.
2. the comiler complained about: var effect = SoundEffect.FromStream(stream); it didn’t recognize SoundEffect.
November 24th, 2011 at 7:24 pm
in 1. i meant, the XAML didn’t recognize the ..not sure why..
January 27th, 2013 at 8:42 pm
[...] you will be able to get the 2 WAV files ! And use them in your app (you can see my older blog post about how to play WAV file in a Silverlight Windows Phone [...]
January 30th, 2013 at 6:31 pm
[...] you will be able to get the 2 WAV files ! And use them in your app (you can see my older blog post about how to play WAV file in a Silverlight Windows Phone app). Hope it [...]