Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Play an embedded wav resource 1

Status
Not open for further replies.

MasterRacker

Active member
Oct 13, 1999
3,343
US
Playing a wav file has turned out to be fairly simple. The following code plays a file just fine if I feed it something like "C:\Test.wav"

Code:
  public class Winmm
  {
    public enum SoundFlags : int 
    {
      SND_SYNC = 0x0000,          // play synchronously (default) 
      SND_ASYNC = 0x0001,         // play asynchronously 
      SND_NODEFAULT = 0x0002,     // silence (!default) if sound not found 
      SND_MEMORY = 0x0004,        // Sound points to a memory file
      SND_LOOP = 0x0008,          // loop the sound until next PlaySound 
      SND_NOSTOP = 0x0010,        // don't stop any currently playing sound 
      SND_NOWAIT = 0x00002000,    // don't wait if the driver is busy 
      SND_ALIAS = 0x00010000,     // name is a registry alias 
      SND_ALIAS_ID = 0x00110000,  // alias is a predefined ID
      SND_FILENAME = 0x00020000,  // name is file name 
      SND_RESOURCE = 0x00040004   // name is resource name or atom 
    }

    [DllImport("Winmm.dll")]
    public static extern bool PlaySound(string Sound, IntPtr hMod, SoundFlags sFlags);

    public Winmm()
    {			
    }

    public static void PlayWavResource(string wav)
    {
      if (!PlaySound(wav, IntPtr.Zero,  SoundFlags.SND_ASYNC | SoundFlags.SND_FILENAME))
      {
        // handle error: unable to play sound 
      }
    }
  }

My problem is that I don't want to haul sound files around with my app. I would prefer to add them to the project as embedded resources like I've done with some bitmaps.

According to the documentation, if I change the flag to SND_RESOURCE, I also need to set the hMod parameter to PlaySound to point to the resource. Unfortunately, I have been unable to find an example of how to do this. Has anyone out there done it this way?

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me
 
Take a look at:


While the example is that of retrieving an image from an embedded resource, it can also be done with a wave file. You then just need to use some code similar to this, which accepts a stream:


Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Between your links and another variation I looked at a while ago (and finally used): my error finally clicked.

The caveat that's not mentioned specifically (but is there if you're paying attention to details like I wasn't): The sound file needs to be embedded in the same project as the playback method since that's what the reflection returns. I have the playback method in a library project and had added the sound to the UI project. I tried hardcoding the other assembly name unsuccessfully and eventually gave up. For my purposes, putting all the sounds in the library module with the playback method will be just fine.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top