Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-05-18
10
911 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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