Solved

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

Posted on 2009-05-18
10
904 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now