Reputation: 27322
I want to play system sounds with .NET code in my app - no problem if I want to use Beep
, Asterisk
etc 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 off by default in the Windows Default Sound Scheme, but if the user has set this sound up to do something then I want to play it.
The user could have assigned any wav file to this action so I want to find which (if any) sound they have assigned and play it. Compatibility with Windows versions back to XP is also essential.
Playing it of course is no issue either as I can use:
My.Computer.Audio.Play(strWAVFile)
So the question is really about how to find it.
Upvotes: 4
Views: 4694
Reputation: 1054
Assuming windows default media folder is in C\Windows\Media
You can simply play any sound that comes with Windows by default.
My.Computer.Audio.Play("c:\windows\media\alarm01.wav", AudioPlayMode.Background)
Have a fresh windows install and open Media folder.
While coding pay attention to target windows compatibility.
Upvotes: 0
Reputation: 12613
The wave sound files for the system's events are stored in the system registry, and they have been there since Windows 95 so compatibility is not an issue.
Check this registry key for the sounds that are played for the events:
HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default
For 'Menu popup' like you said, you can read the default value from this registry key:
HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default\MenuPopup\.Current
So you would 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 soundFile
variable was empty before playing it because not every event might have a sound associated with it and you do not want your application to stop working because of a sound file that cannot be found.
Upvotes: 1