ADDING SOUNDS TO YOUR APPLICATIONS


Sound is a useful way to communicate information to those who use your application. You can use it to alert the user when a task is complete or a specific condition arises. You might, for instance, cause a sound to be played when an application has finished processing data or when a critical error occurs. Visual C++ 2005 Express provides functionality for playing sounds in the form of wave files.

Hint 

A wave file is a digital audio file with a .wav file extension. Wave files store sound information in an uncompressed format. The .wav file format is widely supported on many different operating systems.

You can find wave files in various places. Many can be downloaded from the Internet. You can also find them on your computer. Windows XP, for example, stores several wave files in its Media folder, which is typically located in the Windows folder on your C drive.

Trick 

If you can't locate a wave file that provides the sound you want to play, you can create your own. The Sound Recorder utility and a microphone are all you need. To access the Sound Recorder, click on Start, All Programs, Accessories, Entertainment, Sound Recorder. The Sound Recorder utility appears, as shown in Figure 10.10.

image from book
Figure 10.10: Using the Windows Sound Recorder utility to create a custom wave file.

Working with the SoundPlayer Class

Visual C++ 2005 Express gives you several ways to play sounds within your applications. For starters, you can use the SoundPlayer class, found in the System::Media namespace. This class allows you to play a wave file located on your computer, on a network server, or even on the Internet.

When you reference a file that is stored locally, you must provide a path to the file, in the form of Drive Letter:\Path\WaveFileName.wav. When specifying the location of a wave file located on a network server, identify the location of the file as follows: \\NetworkComputerName\Path\WaveFileName.wav. Finally, to specify the location of a wave file on the Internet, you must specify the file's URL.

Note that when specifying path names, you must store them in a string. To play a wave file stored in the Windows Media folder, for example, you can specify C:\\Windows\\Media\\Chimes.wav as the value for a String variable. This would play the chimes sound when the SoundPlayer's Play() method is called.

Trap 

You must use double backslashes if you want to represent a single backslash within any path you store in a string. In C++, a single backslash followed by certain characters is reserved for special functionality, such as tabbing or beginning a new line.

To use the SoundPlayer class, you must create an instance of it within your program. The SoundPlayer class requires the name of the wave file you want to play, specified as a string containing the full path and file name. After you have provided this, you can use the Play() method to play the wave file. For example, the following code segment demonstrates playing the chimes.wav file found in the Windows\Media folder whenever the user clicks a button:

  1. Load Visual C++ and open a new Windows Forms project.

  2. Add a button to the form and double-click it to generate its click function.

  3. Add the following statements to the newly generated function:

     //Create a string to contain the wave file String^ strWinChimes = gcnew String("C:\\WINDOWS\\Media\\chimes.wav"); //Create an instance of the SoundPlayer System::Media::SoundPlayer sndPlayer(strWinChimes); //Play the wave file sndPlayer.Play(); 

The following are some of the more useful properties and methods of the SoundPlayer class:

  • SoundLocation. A SoundPlayer property that specifies the location of the wave file to be played

  • Play(). A SoundPlayer method that plays the wave file specified by the SoundPlayer class's SoundLocation property

  • PlayLooping(). A SoundPlayer method that repeatedly plays the wave file specified by the SoundPlayer class's SoundLocation property

  • Stop(). A SoundPlayer method that stops playing any currently executing sound

Using the SystemSounds Class

By default, your application has access to certain sounds. The sounds available depend on what functionality your program invokes and the sounds that are specified by the sound scheme on the application user's computer.

Hint 

A sound scheme is a set of sounds that Windows maps to certain events in Windows itself and within the applications it runs.

Some classes and controls automatically utilize sounds when their methods are invoked. The MessageBox class, for instance, automatically triggers a sound when its Show() method is called. In some cases, the sound changes, depending on the parameters passed to the method. If you instruct the MessageBox::Show method to display a dialog box containing a warning icon, for example, it will also play the appropriate warning sound that Windows plays for every warning dialog box.

Trick 

You can pick different sound schemes by opening the Control Panel, selecting Sound, Speech, and Audio Devices, and then choosing Sounds and Audio Devices. This activates the Sounds and Audio Devices dialog box. Sound schemes are located under the Sounds tab.

Visual C++ also allows you to activate a default sound manually. The System::Media::SystemSounds namespace contains functionality to play common Windows sounds such as the Asterisk, Default Beep, and System Notification sounds. You can, for example, cause the Asterisk sound to play whenever the user clicks on a button by adding the following line to your button code:

 System::Media::SystemSounds::Asterisk->Play(); 

Trap 

If no sound exists in the sound scheme for the program event you reference, your code will function properly but fail to play a sound.

The following are some of the program event sounds you can activate:

  • Asterisk. A System::Media::SystemSounds property whose Play() methodtriggers the Windows Asterisk sound. Usually used in dialog boxes as an alert.

  • Beep. A System::Media::SystemSounds property whose Play() method plays the Windows Default Beep sound.

  • Exclamation. A System::Media::SystemSounds property whose Play() method activates the Windows Exclamation sound. Commonly used in dialog boxes to report an error or serious event.

  • Hand. A System::Media::SystemSounds property whose Play() method triggers the Windows Hand sound. Usually signifies a serious error.

  • Question . A System::Media::SystemSounds property whose Play() method invokes the Windows Question sound. Usually used in dialog boxes that query the user or provide information.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net