How to play system sound in XAML?

Here is a simple question, which, to my surprise, I can not find the answer to the question: How to play system sound in XAML?

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

Thanks for your help!

+6
source share
2 answers

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

Interaction 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 don't know if SystemSounds.Beep one you are looking for.)

David Winman Note:

For others studying this issue, the Blend interactivity mentioned in the answer requires a link to System.Windows.Interactivity.dll, which is located in C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries\

+8
source

For completeness, here is the markup I used to solve the HB problem. The markup shows a message in the status bar and plays the sound of System.Asterisk . The message is contained in a StackPanel named StatusBarMessagePanel in the status bar. The message is displayed, then disappears for a five second period.

 <Button ...> <!-- Shows, then fades status bar message. --> <Button.Triggers> <EventTrigger RoutedEvent="Button.Click"> <BeginStoryboard> <Storyboard> <DoubleAnimation From="1.0" To="0.0" Duration="0:0:5" Storyboard.TargetName="StatusBarMessagePanel" Storyboard.TargetProperty="Opacity"/> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> <!-- Note that the following markup uses the custom SystemSoundPlayerAction class, which is found in the Utility folder of this project. --> <!-- Plays the System.Asterisk sound --> <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> 
+1
source

Source: https://habr.com/ru/post/885947/


All Articles