Playing the default Windows sounds from C# used to require InteropServices and system calls. Fortunately, .NET 2.0 includes a new System.Media namespace with three classes that simplify playing system sounds and audio files:

  • SoundPlayer – Plays a .wav file from a file path, URL, Stream or embedded resource.
  • SystemSound – Represents a system sound.
  • SystemSounds – Retrieves and plays system sounds.

To play a system sound, simply call the Play method on one of the static methods (such as Beep) of the SystemSounds class, as shown below in this C# console application:

using System;
using System.Media;
using System.Threading;

namespace SystemSoundsTest
{
    class Program
    {
        static void Main( string[] args )
        {
            Console.WriteLine( "Asterisk" );
            SystemSounds.Asterisk.Play();
            Thread.Sleep( 1000 );

            Console.WriteLine( "Beep" );
            SystemSounds.Beep.Play();
            Thread.Sleep( 1000 );

            Console.WriteLine( "Exclamation" );
            SystemSounds.Exclamation.Play();
            Thread.Sleep( 1000 );

            Console.WriteLine( "Hand" );
            SystemSounds.Hand.Play();
            Thread.Sleep( 1000 );

            Console.WriteLine( "Question" );
            SystemSounds.Question.Play();
            Thread.Sleep( 1000 );
        }
    }
}
Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot