Using the System.Media.SoundPlayer class to play sound
Hi

System.Media Namespace provides many classes to work with the Sound in the application. One of the important classes to play sound is SoundPlayer class. With the help of this class we can simply WAV Files.

The wave files can be loaded by using either the file path, The URL or a stream that contains the .wav file. We can also use an embedded resource that has the .Wav file.

For providing the location of the Sound file we have to use the soundLoaction and the stream property of the SoundPlayer class. The soundLoaction property takes either the file path or the URL of the sound to be loaded. The stream property takes the name of the stream based Object containing the (.Wav) audio to load.

We use the SoundPlayer.Play() method to play the sound.

You can play audio content .wav files synchronously or asynchronously. Calling the Play method asynchronously plays the audio content on a separate thread so your code can continue executing. PlaySync, on the other hand, will play the audio content synchronously and pause program execution until the playback has completed. PlayLooping works the same as Play, but as the name suggest it goes on playing the sound in a loop until the stop method is called to stop the play back.

Here is the code to do the Job

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
Stream SoundResource;
SoundResource = MyApplication.Properties.Resources.DING;
player.Stream = SoundResource;
player.Play();

Here I am loading the Wav from a stream. I have already add the wav file to the resource manager. Now I am picking up the resource into a stream and playing the sound.

Hope this helps
Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 5/30/2007 9:49:05 AM

hi Friend pls help me, I am beginner in C#, I just want to create my own sound playing software with the help of visual studio 2005 and C# language, pls tell me the steps in detail pls

Posted on 1/1/2008 7:37:04 AM

Hi,
I am beginner in ASP.Net. I take a task to make a web site. Client wants play sound in web site. Please help me.
thanking you

Posted on 1/30/2008 8:46:23 AM

Nice job! It worked! thank you

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia