Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I play a System Sound in XAML?

Tags:

wpf

xaml

Here's a simple question that to my surprise, I can't find the answer to: How do I play a system sound in XAML?

I have an event trigger attached to a button. The trigger displays a message, and I want it to play the Windows Notify sound. I have found several references on how to play sound files, but nothing on how to invoke a system sound.

Thanks for your help!

like image 812
David Veeneman Avatar asked Apr 15 '11 01:04

David Veeneman


People also ask

How to play audio in WPF?

WPF has a class called SoundPlayer, which will play audio content based on the WAV format for you. WAV is not a very widely used format today, mainly because it's uncompressed and therefore takes up a LOT of space. So while the SoundPlayer class is simple to use, it's not terribly useful.

What is in XAML?

XAML stands for Extensible Application Markup Language. It's a simple and declarative language based on XML. In XAML, it very easy to create, initialize, and set properties of objects with hierarchical relations.


1 Answers

The SystemSounds class provides some system sounds, they have a Play() method. To use this in XAML you would either have to resort to some whacky hacks, implement lots of custom logic or use Blend Interactivity to define your own TriggerAction which can use a SystemSound and play it.

The Interactivity method:

public class SystemSoundPlayerAction : System.Windows.Interactivity.TriggerAction<Button>
{
    public static readonly DependencyProperty SystemSoundProperty =
                    DependencyProperty.Register("SystemSound", typeof(SystemSound), typeof(SystemSoundPlayerAction), new UIPropertyMetadata(null));
    public SystemSound SystemSound
    {
        get { return (SystemSound)GetValue(SystemSoundProperty); }
        set { SetValue(SystemSoundProperty, value); }
    }

    protected override void Invoke(object parameter)
    {
        if (SystemSound == null) throw new Exception("No system sound was specified");
        SystemSound.Play();
    }
}
<Window 
        xmlns:sysmedia="clr-namespace:System.Media;assembly=System"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
        ...
        <Button Content="Test2">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <i:EventTrigger.Actions>
                        <local:SystemSoundPlayerAction SystemSound="{x:Static sysmedia:SystemSounds.Beep}"/>
                    </i:EventTrigger.Actions>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

(I do not know if SystemSounds.Beep is the one you are looking for.)

David Veeneman's Note:

For others researching this issue, the Blend Interactivity referred to in the answer requires a reference to System.Windows.Interactivity.dll, which is found in C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries\

like image 164
H.B. Avatar answered Oct 20 '22 07:10

H.B.