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:



  1. Right click on your project select “add existing item…“ to open the Add Item dialog box

  2. In the add Item dialog box, change the “File Type” dropdown to show all files

  3. Navigate to the location of your resource, in our case “sound1.wav“ and click “Open“ to add it to your project

  4. With “sound1.wav“ selected, change the “Build Action“ property in the properties window to “Embedded Resource“

  5. 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 (8)

  1. J. Daniel Smith says:

    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.

  2. Thanks for the comment, I’m blogging a response now 🙂

  3. manual coti says:

    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.

  4. Jesse says:

    Where do you create sound1? What type is it?

  5. Navtej Singh says:

    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!

  6. Terry says:

    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 Terry

  7. 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,

    -Dan

  8. frank simari says:

    If you want to see and hear embedded sounds and graphics into pdf files…these do not require a flash player or any other player . You open with any version of adobe reader and have your sound on.