Using the System.Media.SoundPlayer class to play sound
Posted on 9/29/2006 2:40:29 AM
in #Windows Programming
Hi
System/span>.Media/span> Namespace provides many classes to work with the
Sound in the application. One of the important classes to play sound is SoundPlayer/span>
class. With the help of this class we can simply WAV/span> Files./p>
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./p>
For providing the location of the Sound file we have to use
the soundLoaction/span>
and the stream/span>
property of the SoundPlayer/span> class. The soundLoaction/span> property takes
either the file path or the URL of the sound to be loaded. The stream/span>
property takes the name of the stream based Object containing the (.Wav/span>)
audio to load./p>
We use the SoundPlayer.Play()/span> method to play the sound. /p>
You can play audio content .wav files synchronously or
asynchronously. Calling the Play/span> method asynchronously plays the audio content on a
separate thread so your code can continue executing. PlaySync/span>, on the other
hand, will play the audio content synchronously and pause program execution
until the playback has completed. PlayLooping/span> 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./p>
Here is the code to do the Job/p>
System.Media.SoundPlayer player = new System.Media.SoundPlayer();/o:p> Stream SoundResource;/o:p> SoundResource = MyApplication.Properties.Resources.DING;/o:p> player.Stream = SoundResource;/o:p> player.Play();/o:p>/span>/p>
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./p>
/o:p>Hope this helps Thanks Vikram/p>
|
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
/p>
|
Posted on 10/1/2007 5:59:28 AM
hai ,
your explanations to the questions is some what little. please explain with little examples ..
thanku for ur valuable service.
/p>
|
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
/p>
|
Posted on 1/30/2008 8:46:23 AM
Nice job! It worked! thank you
/p>
|
Posted on 5/4/2008 11:52:47 PM
Hai,
how to compare Datetime in between statement in SQL server 2005.
/p>
|
Posted on 1/1/2009 6:04:23 PM
Thanks for posting this. However, playing asynchronous sounds this way can cause crashes, as I found out when I tried to use this code.
/p>
A description of the crash, and a solution can be found here:
http:////www.codeproject.com//KB//audio-video//soundplayerbug/a>.aspx
/p>
|