[WP7] Sound effect in a Silverlight Windows Phone 7 application

A 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:

?View Code CSHARP
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 ?

10 thoughts on “[WP7] Sound effect in a Silverlight Windows Phone 7 application

  1. Hello Jérémy.

    Great code !

    I have just a question : why do you use a TargetedTriggerAction, and not just a TriggerAction ?

  2. 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…

  3. Hello Jeremy,

    coworker is more funny that collaborateur :)
    are touch screen triggers and events available, like fingerOneUp, fingerTwo…?

    have a good day.

  4. 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.

  5. 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.

  6. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>