65.9K
CodeProject is changing. Read more.
Home

System.Media Sound

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.11/5 (11 votes)

Mar 6, 2006

CPOL

1 min read

viewsIcon

52501

downloadIcon

1614

An article on System.Media and how to use simple sounds or *.wav in Windows form application

Sample Image - systemsound.jpg

Introduction

Prior to working with Visual Studio® 2005, adding even simple tunes and system sounds to your application was a challenge. There are numerous new classes and namespaces that have been added to the Microsoft® .NET Framework 2.0 to help you do just that. I'm going to take a look at one of them, the System.Media namespace.

Background

Use System.Media namespace for the following. I will be providing all the code snippets in C#.

    using System.Media;

Using the Code

Use the SystemSounds class to play system defined sounds. This class has five public properties which return an object of SystemSound. The properties are as follows:

  • Asterisk
  • Beep
  • Exclamation
  • Hand
  • Question

Now for playing a Beep sound, one can use the Play function exposed by the SystemSound class.

    //
    // To play a beep
    //
    SystemSounds.Beep.Play();

Use the SoundPlayer class to play wave sounds, be it from file, stream, a URL or an embedded WAV resource. Pass the path of the file/URL in the constructor and load the file asynchronously using the LoadAsync function. One should explicitly load the sound asynchronously if it is from a URL or a stream.

    sndPlayer = new SoundPlayer("http://localhost/test.wav");
    sndPlayer.LoadAsync();

History

  • 6th March, 2006: Initial post