Using the new SoundPlayer class in .NET 2.0 you can easily play WAV files into your application. This tutorial will show you how to create a Windows application that plays WAV audio files in a separate thread or in the UI thread.
Thanks to the new SoundPlayer class of .NET 2.0, playing WAV files takes only a few lines of code. You can start by creating a new Windows application in Visual Studio 2005 and adding to the newly created form, a textbox, four buttons and an OpenFileDialog object.
Name the controls: txtPath for the TextBox and btnBrowse, btnPlay, btnPlayLoop, btnStop for the four buttons. You can name the OpenFileDialog object openWav. The form should look similar to the one below:
Now that we have the form ready, switch to its code and place the following using directive to the new System.Media namespace:
using System.Media;
One other thing we need to do is declare a new SoundPlayer object inside the class:
SoundPlayer spWave;
Let’s now double click the Browse button (btnBrowse) to get to its Click event. Inside it we’re going to use code that shows the OpenFileDialog and allows the user to pick a WAV file:
if (openWav.ShowDialog() == DialogResult.OK)
{
txtPath.Text = openWav.FileName;
}
We assign this path to the TextBox txtPath so that the user can easily see it and modify it in case he wants to play a different file. The next button to double click is the Play button (btnPlay). This too will bring us to its Click event:
if (txtPath.Text != "")
{
spWave = new SoundPlayer(txtPath.Text);
spWave.Play();
}
We simply check for a value inside the TextBox and then create a new instance of the object by passing the path as an argument. At this point in a real-life application if you’ll let the user specify the path to the WAV file, you should check for a valid path and an existing file. The file is then played using the obvious Play() method. This method runs in a separate thread, rendering the form responsive to other commands, and not holding it until the file finished playing. If you want the application to hang and not put the file playing in a different thread than the User Interface one, use the PlaySync() method instead.
To make the file loop there is not a property that you need to set, but to call a different method. Double click the btnPlayLoop button and inside its Click event use the following code, which only differs from the earlier code because of the different method being called:
if (txtPath.Text != "")
{
spWave = new SoundPlayer(txtPath.Text);
spWave.PlayLooping();
}
Finally, there’s the btnStop button to which we haven’t yet assigned any action. Obviously, this will stop the thread that plays the file, and the method is Stop():
spWave.Stop();
You can now compile and test the Play WAV application.
This is all there is to our little application; the SoundPlayer class is itself limited, and you’ll probably want to use something different if you’re going to build a music player. However, if you simply want to add sound capabilities to your application (such as for accessibility), this class will save you a lot of time since you don’t have to search for and use a 3rd party component or managed code like you would do in an older version of .NET Framework.