BlackWaspTM
Audio
.NET 2.0+

Playing WAV Files

A commonly used standard for high quality sound reproduction is the Waveform Audio File Format, with sound data usually stored in WAV files. The .NET framework provides the SoundPlayer class to allow simple playback of such files.

Waveform Audio File Format

The Waveform Audio File Format is a standard format for the storage of audio data. WAV files are ideal for storing high quality sounds though, as files are generally not compressed, the duration of such files is usually short. The format is often used for sound effects and simple user feedback. For example, Microsoft Windows uses WAV files for many system sounds.

SoundPlayer Class

The System.Media namespace contains a set of classes that can be used to play sounds. When you wish to play WAV files, you can use the SoundPlayer class. This includes methods that permit synchronous and asynchronous audio playback and that allow sounds to be preloaded into memory for improved performance.

In this article we will examine the key members of the SoundPlayer class. To begin, create a new Console Application and add the following using directives to the initial class:

using System.ComponentModel;
using System.Media;

Playing WAV Files

To play a WAV file, a SoundPlayer class must first be instantiated and the location of the audio file or resource must be specified. This is achieved by setting the SoundLocation property. The property contains the path or URL to a file containing the sound. Once initialised, the Play method can be executed to play the sound asynchronously.

To demonstrate, add the following code to the Main method. Change the name and path of the WAV file if necessary before executing the program. The call to Console.ReadLine is included to prevent the program from exiting, which would prematurely halt playback of the sound.

SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"C:\Windows\Media\Windows Logon Sound.wav";
player.Play();

Console.ReadLine();

The program can be simplified by using a SoundPlayer constructor that allows the path to the WAV file to be specified as a parameter. To do so, replace the first two lines of code with the following:

SoundPlayer player = new SoundPlayer(@"C:\Windows\Media\Windows Logon Sound.wav");

Playing WAV Files Synchronously

Sometimes you will wish to play sounds synchronously, blocking all other operations on the same thread until the playback is completed. One example is when you wish to play a sound during the shutdown process of an application. If you were to remove the Console.ReadLine line from the previous example code, the asynchronous sound would almost certainly be stopped before it was fully played. If the sound is played synchronously this does not happen.

To demonstrate, replace the contents of the Main method with the following code and execute the program:

SoundPlayer player = new SoundPlayer(@"C:\Windows\Media\Windows Logon Sound.wav");
player.PlaySync();
3 February 2010