Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC

Creating Your Own Sound Alerts

Rate me:
Please Sign up or sign in to vote.
4.06/5 (16 votes)
16 Feb 2003CPOL8 min read 125.8K   46   13
Creating your own Sound alerts which will be displayed in the Sound applet (control panel) and accessed using code.

Sample Image - SoundAlerts.jpg

Introduction

Look at the screenshot above. This window is the Sounds Window from the Control Panel. You can see that there is a new sound type XYZ Alert there. This article is to tell that how can you create your own sound alert type which will appear in the Sounds applet in the control panel and user will be able to customize the sound. This article will also teach you that how can you play that sound using your code.

Disclaimer

This article deals with the registry manipulation. To implement the technique describe here, you must have sufficient knowledge of Registry editing. I will not be responsible of any loss if you do something wrong with your registry.

Registry Information

All the Sound alerts are registered using Windows Registry. You can explore them using Registry Editor. The path of the setting is HKEY_CURRENT_USER\AppEvents. This  key is the base of all settings. Expand this key and you will find Two more keys named EventLabels and Schemes. Let's see the Schemes First and then we will come back to EventLabels

When you will expand the Schemes Registry key under HKEY_CURRENT_USER\AppEvents, you will again get two more keys named App and Names. Under the App key, you can define the Sound Alert Group Type and under the Names key, you can define the Schemes you may have in your system. By default, there are two schemes, Default and No Sound. Default will describe the default sounds of your system (those sounds which are played when you installs a fresh window) and No Sound, the name says all, means No sound will be played if it is selected. These names are defined starting with a period sign (.). Means, there names will be as .Default and .NoSound. Let us create our own scheme to understand it fully.

To create your own Scheme, simply add a new key under HKEY_CURRENT_USER\AppEvents\Schemes\Names key. Let's the name of our new key is .SomethingElse and set the default value of this key to Something Else (or anythin you want). Now go to your control panel and start the Sounds Applet. Expand the Sounds Schemes combo box, and your new scheme Something Else will be there. Note that the name displayed there is Something Else, not the SomethingElse. Which shows that the default value of your new key is considered as the new scheme name, not the key name. But if you do not provide the default value, then you will see an empty index in the Sound Schemes combo box (with no text) in Sounds applet. Note that it is not necessary that your key name for the Sound Scheme starts with a period sign.

Creating Sound Alert Type

Ok, it was very easy to create new Sound Alert Scheme. Now let us move to add our own Sound Alert Type in the sounds. For that follow these steps.

  1. Create a new Key under HKEY_CURRENT_USER\AppEvents\Schemes\App\.Default and name that XYZAlert
  2. Create another key under the key XYZAlert (the key you have created in above step) and name that .default
  3. Set the default value of the .default key to path of some .wav file. eg. C:\abc\abc.wav
  4. Create another key under XYZAlert and name that to .current and also set the path to some wav file, or leave that blank.
  5. Now Create another key under HKEY_CURRENT_USER\AppEvents\EventLabels and name that XYZAlert
  6. Set the default value of this key to anything like "XYZ Alert Here."

That's finish. Now go to your control panel and start the sounds applet. You will see the new sound alert type with name XYZ Alert. Let's discuss what we have made above.

In the first step, we created a new key which will Register the new Sound Alert type. When you were adding that new key, then you may have noticed that there are a number of keys already exists there. Those are the sounds types which are already registered.

In Second Step, you mentioned that the default scheme's sound for this event is the .wav file (the file whose path you mentioned in third step).

In the Fourth step, you mentioned the current sound file name for you new event file. Since it is possible that any user may change the default sound file to something else. In that case, the current file will be used to play.

In the fifth and sixth step, you are mentioning that what the Display name should be for your new sound alert type. For that you will use the same registry key name you created in first step, to create a new key and set the default value to some string which will be displayed in the Sounds applet.

That's all. You have created your own sound alerts. But these alerts are created under the Windows Sound types (the default one). What if you want to create your own Group of sound alerts like Windows Messenger or NetMeeting? To know about that, you have to stay with me.

Creating Group of Sound Alerts

Creating Sound Alert Group is fairly easy as the Creating a single sound alert was. To do the thing, just follow these steps.

  1. Create a new Key under HKEY_CURRENT_USER\AppEvents\Schemes\Apps and name that NewGroup
  2. Set the default value of that key to some name like New Group.
  3. Create another key under the key you created in step one and name that AlertType1.
  4. Create another key under the key you created in above step and name that .current
  5. Set the default value of newly created key to some wav file or leave that blank.
  6. Create another key under the key you created in step two and name that .default
  7. Set the default value of newly created key to some wav file or leave that blank.
  8. Create a new key under HKEY_CURRENT_USER\AppEvents\EventLabels with the same name as in step three and set the default value to some string like Alert Type 1.
  9. Repeat step three to eight for as much sound alert types you want under you new Sound Alert group (NewGroup).

Now go to control panel and start the Sounds Applet to view your sound alert group. You will see there the New Group and new alert types. Let's see what we did above.

In step one, we Created a new Group and in step two, we set the display name for that group.

In step three, we created a new alert type under our new group. Note that the steps from here to onward are same as we did in previous section of this article. Just the difference is that before this we created keys under .default and now we are creating under our new key NewGroup. The theme of new alert type is that you create a key with your new alert, sets the default and current sound files, and finally, set the display name. That's all. I hope everything is fine upto here.

Now the final question which may be striking in your mind that how can we invoke this sound from our application. Is that so? Ok, let's take a look at the final section of this article to know about invoking sound alerts from our application.

Invoking Sound Alerts from Application

Playing sounds from our application is fairly easy. Believe it as I said above that adding new sound alert type is and it was. So, what you need is to call a simple API. The name of that API is sndPlaySound or PlaySound. Let's see the parameters of sndPlaySound.

sndPlaySound takes two arguments, first the sound file name and other is the SoundFlags. Interesting thing is that you can pass the alert name here insted of the path of the Sound File. Like you can call this API as

sndPlaySound "WindowsLogon", SND_NODEFAULT

Where WindowsLogon is the Sound Event Name and SND_NODEFAULT Flag tells the OS that do not play any (like default) sound if in case it couldn't find the Sound mentioned in first parameter. When this statement is invoked, you will listen the Windows Logon Sound mentioned in the Sounds applet. In the same way, you can call your own sound alert using sndPlaySound. Like

sndPlaySound "XYZAlert", SND_NODEFAULT

Note that the name passed here (XYZAlert) is the name of the Registry Key, not the Default value which you defined as display name (XYZ Alert).

In the same way, the PlaySound API can be used to invoke the sound alert, only difference between both APIs is that sndPlaySound always searchs for the System sounds first and then goes for the Path of Sound file while PlaySound API works on the base of third parameter which can be one of SND_ALIAS, SND_FILENAME and SND_RESOURCE. Although it can be combined with other flags as well like SND_NODEFAULT. For more information, see the MSDN for

sndPlaySound
and PlaySound.

Known Issue

There is one problem which I faced during implementation of all of this, that was, If I register my sound alert type using my own Sound Alert Group (as we did in Creating Group of Sound Alerts section of this article) then the sndPlaySound and PlaySound API fails to invoke the sound. While, if the sound type exists inside the Windows Sounds (default sound group) then it works fine. I tried to solve the problem but failed. Frankly, I couldn't spent more time on this problem. All of the knowledge I wrote above is of one day only which means I worked only one day on this topic. If anyone of you got the reason or solution of this problem, then kindly let me know too.

Hope that the information will be sufficient and useful for all the readers to getting started with the Sound Alerts.

Author Information

Sameers (theAngrycodeR) has did his master in computer sciences in Feb. 2002. He proved his expertise in Visual Basic 6 and now working in VB .NET. He is the author of many articles on CodeProject.com as well as on the Microsoft .NET Community Site GotDotNet.com. Also submitted many source codes on Planet-Source-Code.com. The complete information about the author can be found here.

be angry with theAngrycodeR

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGood summary Pin
Bamaco218-Dec-03 8:55
Bamaco218-Dec-03 8:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.