May 22
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 ); } } }
Awww – awesome tip! I’m off to annoy my co-workers!
SystemSounds.Beep.Play();
not working for FireFox Mozila Browser
dat dsnt work!!!!
To play a simple sound use ::
Console.beep(,) –> give d range in b/w 37,32767
or use the soundPlayer from System.Media namespace
instantiate obj for soundplayer ,giving its path (or adding it to resources and giving the resource name) and then calling the Play() function!
Thanks a lot yar. Helped me a lot.
Was searching for this piece of code over the net from long time, finally got it here.