3.7. Play a Windows System Sound
The Windows operating system alerts
users to system events by mapping them to sounds recorded in specific
audio files. The problem is that these files are stored in different
locations on different computers. In .NET 1.0 and 1.1,
there's no easy way to find the default system
sounds and play them in your own application. A new
SystemSounds class in .NET 2.0 addresses this
problem, allowing you to play the most common sounds with a single
line of code.
Note: Need to sound the infamous Windows chime? With the new
SystemSounds class, these audio files are right at your
fingertips.
3.7.1. How do I do that?
The SystemSounds class in the
System.Windows.Forms
namespace provides five shared properties. Each of these properties
is a separate SystemSound object that represents a
specific operating-system event. Here's the full
list:AsteriskBeepExclamationHandQuestion
Once you decide which sound you want to use, you simply need to call
its Play( ) method to play the sound.
Here's an example:
Note: To configure which WAV files are used for each sound,
select the Sounds and Audio Devices icon in the Control
Panel.
SystemSounds.Beep.Play( )
3.7.2. What about...
...playing arbitrary WAV files? The SystemSounds
class works best if you just need an easy way to add a sound for
simple user feedback. If you need to play an audio file of your own
choosing, you need to use the SoundPlayer, as
discussed in the next lab, "Play Simple WAV
Audio."