1. Introduction – Playing Sound
In this example, we will see how we can play sound files in C#. We can play Sound in two specific ways. One way of playing is using the SoundPlayer class from ‘System.Media’ Namespace and the other way is using the ‘Windows Media Player Active-X Control’ called AxWindowsMediaPlayer. In this article, we will try both the ways by playing a ‘.wav file’. You can test yourself for more functionality on the specific area. Now we will start our example.
2. About This Sound Playing C# Example
The below picture shows parts of our example:

SoundPlayer & AxWindowsMediaPlayer Example
The ‘Play’ button will play the sound and ‘Stop’ button will stop it. The example will enabled the ‘Loop It’ checkbox when we click the stop button and it will disable the check box when the sound is playing. When the check box is in the checked state, the sound file will be played repeatedly. Windows Media Player ActiveX control occupies the remaining portion of the windows form. We will play the same sound file using this AxWindowsMediaPlayer ActiveX.
3. Adding Sound as Resource
In the first SoundPlay API Call method, we will use the sound as an application resource. So first we add the sound file to our project and then we drag & drop it to the resource collection. The below youtube video shows this complete process:
Video Steps
- 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. Playing Sound Through SoundPlayer API
4.1 Add System.Media Namespace
Before the form class definition, we place the
System.Media
namespace for playing the sound. In this first approach, we will access the SoundPlayer class to play the sound. Below is namespace inclusion:
1 2 |
//Sample 01: Include the Required Namespace using System.Media; |
4.2 SoundPlayer Private Member
Next, we create a private member of type
SoundPlayer
. We will use this SoundPlayer instance for playing the sound. Below is the code:
1 2 |
//Sample 02: Create Sound Player private SoundPlayer player = new SoundPlayer(); |
4.3 Playing Sound Using SoundPlayer Instance
When the user clicks the play button, we should first disable the check box. Then we will play the sound that we added as the sound resource. We use
SoundPlayer
instance for playing the sound. While playing sound, we check the status of the checkbox and based on the status we play the sound once or repeated. The
Play()
function of the
SoundPlayer
plays the sound file once. To play the sound repeatedly, we use the
PlayLooping()
function of the
SoundPlayer
. Below is the code for that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//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; if (chkLoop.Checked) { player.PlayLooping(); } else { player.Play(); } } |
Note, in the above code, we got in the sound using the
Properties.Resources
and then we assign the sound resource to the
Stream Property
of the
SoundPlayer
object. The
Play()
Method will play the sound once and
PlayLooping()
Method will play the sound file multiple times.
The IntelliSense feature will help us add the resource added to the resource collection. We added ‘song1.wav’ as a sound resource and in the below video one can see accessing the added resource in code with IntelliSense help.
Video Steps
- Moves to the Tag sample 3_2 in the code window.
- Shows IntelliSense help for the added sound resource
4.4 Stopping the SoundPlayer
When the user clicks the stop button, 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:
1 2 3 4 5 6 |
//Sample 04: Stop the Sound Player private void btnStop_Click(object sender, EventArgs e) { player.Stop(); chkLoop.Enabled = true; } |
Note that the above method that is using the SoundPlayer class method which holds good only for playing the wav files. To play other sounds or even videos, we should use the ‘Windows Media Player Control’ ActiveX known as AxWindowsMediaPlayer.
5. Playing Media Files Using AxWindowsMediaPlayer
Windows Media Player control is not a standard control. This is usable as an active-x control and it will be free when you installed Windows Media Player in your system. The active-x control name is AxWindowsMediaPlayer. AxWindowsMediaPlayer control supports rich functions, but for this example, we will try a simple function of playing the sound file.
The below video shows how to add AxWindowsMediaPlayer to windows form:
Video Steps
- 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 (AxWindowsMediaPlayer) 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, we change its default name to ‘WMPlay’. After that we add the below piece of code in the form handler:
1 2 3 4 5 6 7 |
//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; } |
Code Explanation
In the form load, we set the
autoStart Property
of the control 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 set the
URL Property
for the AxWindowsMediaPlayer. The URL is nothing but the path to the sound file. Here in the above code, we don’t specify any path so AxWindowsMediaPlayer Control will assume that the file will be available in the same location where the EXE is placed.
When the form is opened, the AxWindowsMediaPlayer Control is ready to play the loaded sound file. All it need is someone pushing the play button on it. Once it is pressed AxWindowsMediaPlayer plays the sound. The Windows Media Player Active-x control (AxWindowsMediaPlayer) is a rich control and one can play various types of sounds and a video file.
Download Source Code From Google Drive: Play Sound in C# Windows From – Example
Categories: C#
Tags: autoStart Property, AxWindowsMediaPlayer, Play(), PlayLooping(), SoundPlayer, Stop() method, Stream Property, URL Property