How to play non-standard system sounds in .NET.

I want to play system sounds using .NET code in my application - not a problem if I want to use Beep, Asterisketc., as I can just use:

My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Asterisk)

But what if I want to play something like "Menu Pop-up"? This sound is turned off by default in the Windows sound scheme by default, but if the user set this sound to do something, I want to play it.

The user could assign any wav file for this action, so I want to find what (if any) sound they assigned and play it. Compatibility of Windows versions with XP is also important.

Playback, of course, is not a problem since I can use:

My.Computer.Audio.Play(strWAVFile)

So the question is how to find it.

+3
source share
2 answers

Wave sound files for system events are stored in the registry and they were there with Windows 95, so compatibility is not a problem.

Check this registry key for sounds that play for events:

HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default

For the pop-up menu, as you said, you can read the default value from this registry key:

HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default\MenuPopup\.Current

So you should write code like this:

Dim rk = Registry.CurrentUser.OpenSubKey("AppEvents\Schemes\Apps\.Default\" & _
                                         "MenuPopup\.Current")
Dim soundFile = rk.GetValue("")

If soundFile <> "" Then 
    My.Computer.Audio.Play(soundFile)
End If

I checked if the variable was soundFileempty before playing it, because not every event can have a sound associated with it, and you do not want your application to stop working due to a sound file that cannot be found.

+1
source

Assuming the default Windows media folder is in C\Windows\Media

, Windows .

 My.Computer.Audio.Play("c:\windows\media\alarm01.wav", AudioPlayMode.Background)

Media.

.

0

All Articles