Solved

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

Posted on 2009-05-18
10
910 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:HawaiiDragon
  • 5
  • 3
  • 2
10 Comments
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24415330
huh. I've never tried this in .net!

Does this help?

http://www.eggheadcafe.com/articles/20030302.asp
0
 

Author Comment

by:HawaiiDragon
ID: 24415690
not really....  unfortunaly
0
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24415695
ok
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:HawaiiDragon
ID: 24415757
it works in his soluition but not mine.... quite annoying
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24415850
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
 

Author Comment

by:HawaiiDragon
ID: 24415993
okay how do I add the sound clip "whip.wav" as a resource becasue I still cant get it to work.
0
 

Author Comment

by:HawaiiDragon
ID: 24416167
please see the doc file for full details. Let me know what ive done wrong I feel like such an idiot.

problems.doc
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24416820
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
 

Author Closing Comment

by:HawaiiDragon
ID: 31582724
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24417075
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question