Embedding sound files into a Windows app (Whidbey)
I have to confess: I've been coding again...its my one escape from the 104 flagged emails that need responses or large amounts of work done, and the as-yet uncovered firedrills which I'm sure are just one Outlook-sync away. For now, I blog...
Recipe: Embed a sound file that will play when a Windows form application is loaded.
Pre-cooking steps
- Open Whidbey M2 PDC (or greater)
- Create a new Windows Form application
- Have Solution Explorer and the Properties Window visible in Visual Studio
Step 1 - Add and Embed your resource
Windows Forms enables you to embed resources (like images, icons, sound files, etc) directly into your application. To embed a resource, all you have to do is:
- Right click on your project select “add existing item...“ to open the Add Item dialog box
- In the add Item dialog box, change the “File Type” dropdown to show all files
- Navigate to the location of your resource, in our case “sound1.wav“ and click “Open“ to add it to your project
- With “sound1.wav“ selected, change the “Build Action“ property in the properties window to “Embedded Resource“
- This resource will now be embedded directly into the exe.
Step 2 - Using an embedded resource
- Since we want to play a sound file when the form loads, simply drag and drop the new Sound control from the toolbox onto your Windows form application. This will create an object named sound1 that we will programatically set our embedded sound source to.
- Open up the Form Load event and type
Stream _sound = this.GetType().Assembly.GetManifestResourceStream ("MyAssemblyName.sound1.wav");
sound1.Stream = _sound;
sound1.Play(); - In the first line, I create a stream object that will contain the contents of my sound file, and I use reflection to pull in the stream from a particular file in the assembly using the AssemblyName.FileName naming convention. Next I'll set the Stream property for Sound1 to point to our wav file, and then play the sound. Done :)
- If this was a picture file, you could drag and drop a PictureBox onto the control and add the following code.
Stream _picture = this.GetType().Assembly.GetManifestResourceStream("MyAssemblyName.pic1.jpg");
System.Drawing.Bitmap pic = new System.Drawing.Bitmap(_picture);
pictureBox1.Image = pic;
Gasp, Outlook says I have to go, but that's pretty much it for simple scenarios, I'll blog more later.
Comments
Anonymous
February 19, 2004
Why doesn't the new Sound control support WMA files? (Yes, I know it's just a wrapper around the Win32 control...)
But still, this "mixed messaging" from Microsoft is a bit annoying at times: use WMA (and not MP3)...unless you want to embed it in your application, then you have to use WAV.Anonymous
February 19, 2004
Thanks for the comment, I'm blogging a response now :)Anonymous
July 13, 2005
the best i can do is to put a bunch of jpegs, gifs or words into a pdf file; then embed a sound into each part. if you want a sample, send me a jpeg and a not too long sound (few seconds). i will try to get it together for you. your email will be necessary, not your name. the sounds will play with a mouseover. this offer ends on july31 2005. no other software other than the adobe freeware everyone has will be necessary to play it.Anonymous
March 20, 2006
Where do you create sound1? What type is it?Anonymous
February 26, 2007
I really feel that I should express my sincere thanks for all the help I get from Computer Gurus like your goodself. Keep it up, sir. Thanks once again!Anonymous
March 10, 2008
Hi Dan, Thanks for your detailed description on how to embed sound files into a Windows application but I need more assistance and would appreciate any help you can give. Would you know how I can do a similar thing using Visual Basic .NET rather than C#. Many thanks TerryAnonymous
March 11, 2008
Hey Terry, I just tested this using VB 2008 Express Edition. An easier way to load this in VB is to right click on the project from Solution Explorer and click Properties (or from the Project Menu, select "[projectname] Properties...". Step 1 - Add the wave file to a Windows Form project click Add Resource... and navigate to the *wav file you're looking for. Step 2 - Drag and drop a button on the form and double click the form to build it's event handler Step 3 - Write the code Here's the code in its entirety. Notice that you do get IntelliSense for embedded resources in VB which is really nice. The SoundPlayer class is in "System.Media" so you need to explicitly add an imports at the top. The wav file I'm using is named "damage1" Imports System.Media Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Create a sound player Dim s As New SoundPlayer() 'set the stream to the embedded wav file name s.Stream = My.Resources.damage1 'play the sound s.Play() End Sub End Class Cheers, -DanAnonymous
April 10, 2008
The comment has been removed