Playing a sound on a button click in a windows program C#

Hi all:
 Im trying to play a spcific sound on a button click. I have tried creating a method to do so but its not working. Any help would be great.
HawaiiDragonAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
In the Solution Explorer, select "Whip.wav".  Now down below, in the Properties Pane, change the "Build Action" dropdown to "Embedded Resource".

Then use code like this:
        private void button1_Click(object sender, EventArgs e)
        {
            string EmbeddedWav = "Whip.wav";
            System.Reflection.Assembly assm = System.Reflection.Assembly.GetExecutingAssembly();
            System.Media.SoundPlayer sp = new System.Media.SoundPlayer(assm.GetManifestResourceStream(assm.GetName().Name + "." + EmbeddedWav));
            sp.Play();
        }

Open in new window

0
 
ToddBeaulieuCommented:
huh. I've never tried this in .net!

Does this help?

http://www.eggheadcafe.com/articles/20030302.asp
0
 
HawaiiDragonAuthor Commented:
not really....  unfortunaly
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ToddBeaulieuCommented:
ok
0
 
HawaiiDragonAuthor Commented:
it works in his soluition but not mine.... quite annoying
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Assuming the wav file is in the same folder as your EXE:

        private void button1_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer sp = new System.Media.SoundPlayer("yeehaw.wav");
            sp.Play();
        }

See SoundPlayer():
http://msdn.microsoft.com/en-us/library/system.media.soundplayer(VS.80).aspx
0
 
HawaiiDragonAuthor Commented:
okay how do I add the sound clip "whip.wav" as a resource becasue I still cant get it to work.
0
 
HawaiiDragonAuthor Commented:
please see the doc file for full details. Let me know what ive done wrong I feel like such an idiot.

problems.doc
0
 
HawaiiDragonAuthor Commented:
Your a god. Now my question is why did this work and not the others. Is it because I did not make it an embedded resource? Anything I can learn is appriciated.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
With my previous submission, the wav file would have to be physically located in the same folder as the running .net exe.  As an embedded resource it is contained within the EXE and always there.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.