Playing Sounds in .NET 2.0 Using Sound Player

If you have played .wav files in previous versions of .NET, you must be familiar with PlaySound() Win32 API function. To use the PlaySound function, first you must import the "winmm.dll" and then define the function as following:

[sysimport(dll="winmm.dll")]
public static extern long PlaySound(String lpszName, long hModule, long dwFlags);

And you can the function like this:

PlaySound( szWavFileName , 0 , 0);

Now good news for developers is, there us a SoundPlayer class in .NET 2.0, which is a managed wrapper for the above function.

The SoundPlayer Class

The SoundPlayer class is used to load and play a .wav file in .NET. You can load a file from a file path, a URL, or even from a steam.

The code in Listing 1 creates a SoundPlayer object, sets the .wav file using the SoundLocation property of SoundPlayer and calls the Play method. See the attached source code for complete details.

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
SoundPlayer player = new SoundPlayer();
player.SoundLocation = openFileDialog1.FileName;
player.Play();
}

Listing 1. Loading and playing a .wav file.

Synchronous versus Asynchronous

You can load and play a .wav file using the above method listed in Listing 1, which is called synchronous method. However, the file needs to be loaded before it can be played and if there is a large file, there might be a wait. And if you do not want to wait for the sound to be played, you can load the .wav file asynchronously.

The code listed in Listing 2 shows how to load and play a file asynchronously.

/// <summary>
/// SoundPlayer
/// </summary>
private SoundPlayer player = new SoundPlayer();
/// <summary>
/// Button click event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AsyncBtn_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Set .wav file as TextBox.Text
textBox1.Text = openFileDialog1.FileName;
// Add LoadCompleted event handler
player.LoadCompleted += new AsyncCompletedEventHandler(LoadCompleted);
// Set location of the .wav file
player.SoundLocation = openFileDialog1.FileName;
// Load Asynchronously
player.LoadAsync();
}
}
/// <summary>
/// LoadCompleted event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void LoadCompleted(object sender, AsyncCompletedEventArgs args)
{
player.Play();
}

Listing 2. Load and Play .wav asynchronously

System Sounds

The SystemSounds class is used to play system sounds such as Beep. The following image shows the members of the SystemSounds class.  

SystemSoundsImg1.gif

The following code plays a beep sound:

SystemSounds.Beep.Play();

Summary

Finally, we have a managed class that may be used to play sounds in .NET. Even though, it is a wrapper around PlaySound() Win32 function, still it is easier to use then the PlaySound() function. In this article, I discussed, how we can use this class and its functionality in our applications.

Up Next
    Ebook Download
    View all
    Learn
    View all