We will see how we can play sound files in C#. Sound can be played in two different ways. One way of playing is using the "System.Media Namespace" and the other way is using the "Windows Media Player Active-X Control".
In this article, I will cover both the methods by playing a ".wav file". You can explore yourself for more functionality on the specific area. OK, Let us start.
2. About the Sample
The sample application’s screenshot is shown below:
The Play button will start playing the sound and stop button will stop it. Loop It checkbox will be enabled when you click the stop button and it is disabled when the sound is playing. When the check box is in the checked state the sound file will be played repeatedly. Next portion of the form is occupied by the Windows Media Player ActiveX control which plays the same sound file played by the sound player.
3. Adding sound as Resource
In the first method, we are going to use the sound as an application resource. So first we added the sound file to our project and then it is drag and dropped to the resource collection. This whole step is shown in the below video.
- Create a folder for sound
- Add a single sound wav file to the sound folder in project explorer
- Drag and Drop this added sound file to the application’s resource collection.
4. Source Code for using SoundPlayer
1) Before the form class definitions, use the required namespace for playing the sound. In the first method, we are going to access the SoundPlayer class to play the sound. Below is namespace inclusion:
//Sample 01: Include the Required Namespace
2) Next, a private member of type SoundPlayer is created. We will use this instance player for playing the sound. Below is the code:
//Sample 02: Create Sound Player
private SoundPlayer player = new SoundPlayer();
3) When the play button is clicked first we should disable the check box. Then we will play the sound that we added as the sound resource. While playing sound we check the status of the checkbox and based on the status we play the sound once or repeated. Below is the code for that:
//Sample 03: Play button handler
private void btnPlay_Click(object sender, EventArgs e)
//Sample 03_1: Disable the Checkbox button
chkLoop.Enabled = false;
//Sample 03_2: Play the wav or do play looping
player.Stream = Properties.Resources.song1;
In the above code, we accessed the sound using the "Properties.Resources" and the sound resource is assigned to the "Stream Property" of the player object. The "Play() Method" will play the sound once and "PlayLooping() Method" will play the sound file multiple times.
The IntelliSense feature will help you add the resource added to the resource collection. We added song1.wav as a sound resource and in the below video you can see accessing the added resource in code with IntelliSense help.
- Move to the Tag sample 3_2 in the code window
- Shows IntelliSense help for the added sound resource
4) When the stop button is clicked we call Stop method of the player and in the meantime enable the check box so that play mode can be changed. Below is code for the stop method:
//Sample 04: Stop the Sound Player
private void btnStop_Click(object sender, EventArgs e)
chkLoop.Enabled = true;
Note that the above method that is using the SoundPlayer class method will support only wav files. To play other sounds or even videos we should use the "Windows Media Player Control".
5. The Windows Media Player Control
Windows Media Player control is not a standard control. This is available as an active-x control and it will be available when you installed Windows Media Player in your system. This control supports rich functions, but for this sample, I just gave the simple function of playing the sound file.
Look at the below video that shows how the window media player control is added to the form.
- From the toolbox general area, choose items option is selected using the context menu.
- In the displayed dialog, COM components tab opened
- Window media player control is selected from the bottom of the list
- The Component added to the toolbox is placed on the form for use
After placing the control its default name is changed to WMPlay. In the form load the below piece of code is added:
//Sample 05: Set Sound URL for the Media Player
private void frmSound_Load(object sender, EventArgs e)
string URL = "song1.wav";
WMPlay.settings.autoStart = false;
WMPlay.URL = URL;
In the form load, the "autoStart Property" of the control is set to false. That means, even though the control is aware of what it needs to play, it will wait for the user to say “OK, Now you play the loaded file”. In the form load after turning off the Auto Start, we specified the URL, which is nothing but the path to the sound file. Here in the above, I don’t specify any path so it will assume that the file will be available in the same location where the exe is placed.
When the form is displayed, the media player is ready to play the loaded sound file. All it need is somebody pressing the play button on it and once it is pressed media player start playing the sound.
The Window media player active-x control is a rich control and you can play different types of sound as well as a video file.
Source Code : Download