Solved

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

Posted on 2009-05-18
10
914 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

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

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 86

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 86

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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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