Home > Guides > Programming > .NET and Windows Programming

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Playing Sounds with .NET 2.0

Last updated Apr 28, 2006.

The .NET 2.0 Framework Class Library includes a new class in the System.Media namespace, called SoundPlayer. SoundPlayer provides a simple interface for loading and playing a .wav file. This is a much nicer and more robust interface than the PlaySound interface that programmers used in previous versions and is described in Playing Simple Sounds.

Loading a Sound

To load a sound, first you have to set the location from which the sound should load. You must set the SoundPlayer’s SoundLocation or Stream property to tell it from where to load the data. After setting the location, you must then call Load or LoadAsync to load the sound into memory.

You don’t have to load the sound before you play it. The Play and PlaySync methods will load the sound if you haven’t loaded it already. However, you get much better control over your program’s behavior if you load the sound first.

SoundPlayer can load a sound from a file path, any stream that contains a .wav file, a URL, or an embedded resource. In addition, it can load the sound synchronously or asynchronously. When loading synchronously, the user program will block until the Load method returns. The Load method will likely produce a noticeable delay in a Windows Forms application if you try to load a large .wav file. For larger files, you should use LoadAsync.

If you call LoadAsync to load a .wav file, it will raise the LoadCompleted event when done, even if the load failed. The AsyncCompletedEventArgs object instance that is passed to the event handler will indicate whether or not the file loaded successfully.

You can set the LoadTimeout property to specify the amount of time, in milliseconds, the program should wait for the load to complete. If the load does not complete in this amount of time, the loading is canceled and the object throws a TimeoutException. The default timeout value is 10 seconds (10,000 milliseconds). Setting this value to 0 will prevent timeouts, but your program could wait indefinitely (i.e., "hang") if it is unable to load a sound.

The SDK Article How to: Load a Sound Asynchronously within a Windows Form provides an excellent example of loading a sound using LoadAsync.

Playing a Sound

You can play a sound synchronously or asynchronously. If you play the sound synchronously by calling the PlaySync method, the sound is played on the main thread. In a Windows Forms application, this means that the user interface will be unresponsive while the sound is playing. You should almost never use PlaySync.

The most common way to play a sound is to call Play. This will play the sound on a new thread, leaving the main thread free to perform other tasks. The sound will play one time and then stop.

PlayLooping will play the sound asynchronously on a new thread, in an infinite loop. The sound will stop playing when Stop is called, or when the SoundPlayer object instance is destroyed.

All of the Play functions will load the sound if it has not already been loaded. PlaySync loads the sound synchronously. Play and PlayLooping load the sound asynchronously in the background.

Two SDK articles, How to: Play a Sound from a Windows Form, and How to: Loop a Sound Playing on a Windows Form have very good examples of playing sounds from Windows Forms programs.

The SoundPlayer class is a much better sound interface than the old PlaySound Windows API interface that we had to use for .NET 1.1 and earlier applications. However, SoundPlayer still works only with .wav files. We still must use third party components or deal with the more complicated Windows media interfaces in order to play .mp3 and other types of media files.

Discussions

Copies of the array?
Posted Dec 23, 2008 03:40 PM by luige21
1 Replies
Hi
Posted Dec 5, 2008 05:10 AM by ajay2000bhushan
2 Replies
You have no clue.
Posted Jun 10, 2008 03:28 PM by theinternetmaster
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jim Mischel"Highly unlikely" does not mean "impossible"
By Jim MischelJuly 18, 2009 No Comments

One of my programs crashed the other day in a very unexpected place.  A call to System.Threading.ConcurrentQueue.TryDequeue (from the Parallel Extensions to .NET) resulted in an OverflowException being thrown.  Investigation revealed a pretty serious bug in the System.Random constructor.

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part II
By John TraenkenschuhMay 24, 2009 No Comments

In the last blog in this series, Traenk relates his first experiences with computers and with coding.  But now, some years have passed. . .

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part I
By John TraenkenschuhMay 24, 2009 No Comments

Traenk relates his past experience with Operating Systems that goes back 25 years, ok, more than that but he ain't tellin'

See More Blogs

Informit Network