The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Embedding sound files into a Windows app (Whidbey)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Dan Fernandez

Posts: 456
Nickname: danielfe
Registered: Aug, 2003

Daniel Fernandez is the Product Manager for C# in the developer division at Microsoft.
Embedding sound files into a Windows app (Whidbey) Posted: Feb 19, 2004 5:16 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Dan Fernandez.
Original Post: Embedding sound files into a Windows app (Whidbey)
Feed Title: Dan Fernandez's Blog
Feed URL: /msdnerror.htm?aspxerrorpath=/danielfe/Rss.aspx
Feed Description: Dan discusses Dot Net.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Dan Fernandez
Latest Posts From Dan Fernandez's Blog

Advertisement

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.

 

Read: Embedding sound files into a Windows app (Whidbey)

Topic: Choosing the best computer for you...in 1982. Previous Topic   Next Topic Topic: Test Coverage IS important...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use