Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I play the Windows Notification sound? (It is not defined in System.Media.SystemSounds)

Tags:

c#

windows

audio

When using System.Media, there is something called SystemSounds where you can easily play a couple of operating system sounds:

System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Hand.Play();
System.Media.SystemSounds.Question.Play();

Unfortunately, there are only these five options, and in Windows 10, three of them are the same while one of them doesn't even play anything.

What I really want to do is play the Notification sound, as defined in the Sound panel (seen here):

(imgur: Notification sound in the Sound Panel)

Does anyone know how to do this?

like image 635
FNG Avatar asked Dec 29 '17 16:12

FNG


1 Answers

Solution found. Code here:

using System.Media;
using Microsoft.Win32;

public void PlayNotificationSound()
{
    bool found = false;
    try
    {
        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\.Default\Notification.Default\.Current"))
        {
            if (key != null)
            {
                Object o = key.GetValue(null); // pass null to get (Default)
                if (o != null)
                {
                    SoundPlayer theSound = new SoundPlayer((String)o);
                    theSound.Play();
                    found = true;
                }
            }
        }
    }
    catch
    { }
    if (!found)
        SystemSounds.Beep.Play(); // consolation prize
}

You can browse the keys in the registry editor to see the other sounds. Also, this example is coded to work for Windows 10, and I'm not sure what the registry structure is for other versions of Windows, so you'll need to double check what OS the user is using if you're trying to code for multiple platforms.

like image 156
FNG Avatar answered Oct 22 '22 10:10

FNG