Solved

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

Posted on 2009-05-18
10
912 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
[X]
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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

761 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