SIGN UP MEMBER LOGIN:    
ARTICLE

Playing Sounds in .NET 2.0 Using Sound Player

Posted by Mahesh Chand Articles | Windows Forms C# February 13, 2006
If you have ever played sounds in your applications, you must be familiar with PlaySound() WIN32 function. Prior to .NET 2.0, there was no way to play sounds thorugh the managed API. The SoundPlayer class introduced in .NET 2.0 now allows developers to play sounds using managed API.
Reader Level:
Download Files:
 

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.

Login to add your contents and source code to this article
share this article :
post comment
 

question1:the error show me when use AsyncBtn_Click the error is [Sound API only supports playing PCM wave files.] can any help me question2: i want to play sound during the sound is recorded such as online Hear the method Async is work in this case please help me my email is hani_t_hassan@hotmail.com

Posted by hani hassan May 19, 2011

Thanx Bro;
It really works !

Posted by Charitha Gunathilake Jan 16, 2010

thnks sir but i have created a multi player by importing [DllImport("winmm")] which is a wmp dll file... by this i can play all media files..... thnks

Posted by Nikhil Kumar Apr 10, 2009

Hmm .. I don't think so.

Posted by Mahesh Chand Apr 02, 2009

Search this site for media player. To play MP3,you need to use a media player.

Posted by Mahesh Chand Apr 02, 2009
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor