Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Playing Sound Via C# SoundPlayer & AxWindowsMediaPlayer

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

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

  1. Create a folder for sound.
  2. Add a single sound wav file to the sound folder in project explorer.
  3. 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:

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:

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:

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

  1. Moves to the Tag sample 3_2 in the code window.
  2. 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:

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

  1. From the toolbox general area, ‘choose items’ option is selected using the context menu.
  2. In the displayed dialog, ‘COM components tab’ opened.
  3. Window media player control (AxWindowsMediaPlayer) is selected from the bottom of the list.
  4. 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:

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: , , , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.