Selecting sounds from Windows and playing them
Asked Answered
P

4

15

I have a WinForms app. This app has a Preferences section where the user will be able to select which sounds are played when an alert is being displayed.

Is it possible to have a combobox where the user can select from the Windows stored sounds such as "critical stop", "critical beep" and so on. These are found in the "Control Panel" >> "Sounds and Alerts" section.

Is it also possible to have a play button to test the sounds out?

Penick answered 4/3, 2011 at 11:54 Comment(1)
The set of sounds are extensible, maybe you noticed the Visual Studio build sounds? Ask at superuser.com where they are stored in the registry. Do try to avoid redesigning standard parts of Windows.Sydelle
C
14

Try this:

    private void Form1_Load(object sender, EventArgs e)
    {

        var systemSounds = new[]
                              {
                                  System.Media.SystemSounds.Asterisk,
                                  System.Media.SystemSounds.Beep,
                                  System.Media.SystemSounds.Exclamation,
                                  System.Media.SystemSounds.Hand,
                                  System.Media.SystemSounds.Question
                              };

        comboBox1.DataSource = systemSounds;

        comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
    }

    void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ((System.Media.SystemSound)comboBox1.SelectedItem).Play();
    }
Cota answered 4/3, 2011 at 13:19 Comment(0)
D
19

You do not require any API to play system sounds just write code like this:

// Plays the sound associated with the Asterisk system event.
System.Media.SystemSounds.Asterisk.Play();

The SystemSounds class contains the following predefined system sounds:

  • Asterisk
  • Beep
  • Exclamation
  • Hand
  • Question

All other sounds require you read the desired sound from the registry and play it with code like this:

SoundPlayer simpleSound = new SoundPlayer(@"c:\Path\To\Your\Wave\File.wav");
Dyann answered 4/3, 2011 at 12:12 Comment(0)
C
14

Try this:

    private void Form1_Load(object sender, EventArgs e)
    {

        var systemSounds = new[]
                              {
                                  System.Media.SystemSounds.Asterisk,
                                  System.Media.SystemSounds.Beep,
                                  System.Media.SystemSounds.Exclamation,
                                  System.Media.SystemSounds.Hand,
                                  System.Media.SystemSounds.Question
                              };

        comboBox1.DataSource = systemSounds;

        comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
    }

    void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ((System.Media.SystemSound)comboBox1.SelectedItem).Play();
    }
Cota answered 4/3, 2011 at 13:19 Comment(0)
S
8

Sure! All the sounds you're looking for are available through the System.Media.SystemSounds class, where they are exposed as public properties corresponding to the event types that trigger the sounds.

Additionally, objects of the SystemSound class provide a Play method that you can call to play that sound asynchronously.

So for example, to play the "Critical Stop" sound, you would simply write the following code:

System.Media.SystemSounds.Hand.Play();
Selfcontradiction answered 4/3, 2011 at 12:13 Comment(0)
S
3

SystemSounds only cover a few sounds. For playing the others, you need to read the registry :

const string key = @"AppEvents\Schemes\Apps\.Default\Notification.Default\.Default";
using (var reg = Registry.CurrentUser.OpenSubKey(key))
using (var player = new SoundPlayer((string)reg.GetValue(string.Empty)))
{
    player.Play();
}
Stemware answered 11/3, 2022 at 23:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.