BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

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();

Playing a Sound Repeatedly

The third way to play a WAV file is using the PlayLooping method. This plays a sound asynchronously and repeatedly. Once the entire sound has been played, it is restarted. The sound continues until the program exits or the Stop method is called.

The following sample code plays a looping sound for five seconds:

SoundPlayer player = new SoundPlayer(@"C:\Windows\Media\Windows Logon Sound.wav");
player.PlayLooping();
System.Threading.Thread.Sleep(5000);
player.Stop();

Console.ReadLine();

Preloading Sounds

Each of the three Play methods loads the sound when called for the first time. This can be problematic if the sound file is very large as there can be a noticeable pause between calling the method and the sound being heard whilst the file loads. This pause can be prevented by preloading the sound using the Load method. This method retrieves the audio file synchronously, ensuring that it is available for subsequent, immediate use.

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

When the preloading of a sound file completes, a LoadCompleted event is raised. In the following example, the event is captured and the sound is played within the handling method. This adds little to the functionality of the previous example. However, it is useful should you modify the program to preload sounds asynchronously.

static void Main(string[] args)
{
    SoundPlayer player = new SoundPlayer(@"C:\Windows\Media\Windows Logon Sound.wav");
    player.LoadCompleted += new AsyncCompletedEventHandler(player_LoadCompleted);
    player.Load();
    Console.ReadLine();
}

static void player_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
    SoundPlayer player = (SoundPlayer)sender;
    player.Play();
}

Preloading Sounds Asynchronously

Preloading sounds synchronously ensures that there is no delay when one of the Play methods is called. However, if there are lots of sound files or several large files to preload, the loading time can make the program unresponsive or can increase the overall start-up time of an application. One solution is to load the sounds asynchronously. To demonstrate, replace the Load method in the previous example with the LoadAsync method shown below. As before, this loads the sound and then raises the LoadCompleted event.

player.LoadAsync();
3 February 2010