Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to play WAVE sound from resource?

How to play WAVE sound from resource?

Scheduled Pinned Locked Moved C#
tutorialhardwarequestionlearning
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Szymon Pusz
    wrote on last edited by
    #1

    I put a WAVE file into the resource as a Embedded resource. How to play it now? Could someone give me an example?

    J 1 Reply Last reply
    0
    • S Szymon Pusz

      I put a WAVE file into the resource as a Embedded resource. How to play it now? Could someone give me an example?

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      Since there isn't any managed methods for playing sounds you'll have to load the file from the stream into memory, then call the appropriate API function to do the actual playing. To get the stream this should work.

      public byte[] GetWave(string filename)
      {
      Assembly asm = GetType().Assembly;

      Stream stream = asm.GetManifestResourceStream(filename);
      byte [] wav = new byte[stream.Length];

      stream.Read(wav, 0, wav.Length);

      return wav;
      }

      Untested but it should work since I wrote almsot the exact same code in another program. I'm not familiar with the sound playing API though so you'll have to reference that on your own. James Simplicity Rules!

      S 1 Reply Last reply
      0
      • J James T Johnson

        Since there isn't any managed methods for playing sounds you'll have to load the file from the stream into memory, then call the appropriate API function to do the actual playing. To get the stream this should work.

        public byte[] GetWave(string filename)
        {
        Assembly asm = GetType().Assembly;

        Stream stream = asm.GetManifestResourceStream(filename);
        byte [] wav = new byte[stream.Length];

        stream.Read(wav, 0, wav.Length);

        return wav;
        }

        Untested but it should work since I wrote almsot the exact same code in another program. I'm not familiar with the sound playing API though so you'll have to reference that on your own. James Simplicity Rules!

        S Offline
        S Offline
        Szymon Pusz
        wrote on last edited by
        #3

        Thanks, but does anyone knows how to open these wave bytes??? I know that there's an API function called PlaySound, but it can play from file or from *Win32* resource. As far as I'm concerned C# doesn't compile resources as a Win32 type, but as it own format... Any ideas?

        N 1 Reply Last reply
        0
        • S Szymon Pusz

          Thanks, but does anyone knows how to open these wave bytes??? I know that there's an API function called PlaySound, but it can play from file or from *Win32* resource. As far as I'm concerned C# doesn't compile resources as a Win32 type, but as it own format... Any ideas?

          N Offline
          N Offline
          Neil Van Note
          wrote on last edited by
          #4

          This is one of those areas where I can't help but think *there has to be a better way*... If you take Mr. Johnson's GetWave method above and combine it with something like this...

          	public static void PlaySoundMemory(string resid)
          	{
          		// Stop current sound
          		PlaySound(
          			IntPtr.Zero,
          			IntPtr.Zero,
          			PlaySoundFlags.SND\_SYNC
          			);
          		// Play sound
          		byte\[\] buffer = GetWave(resid);
          		if (null != buffer)
          		{
          			IntPtr p = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
          			PlaySound(
          				p,
          				IntPtr.Zero,
          				PlaySoundFlags.SND\_SYNC | PlaySoundFlags.SND\_MEMORY
          				);
          		}
          	}
          

          You end up with something that works. You could also write the resource to a temp file and play it directly from there. The PlaySound Function and the PlaySoundFlags are just API definitions lifted from the Platform SDK headers. Regards

          S 1 Reply Last reply
          0
          • N Neil Van Note

            This is one of those areas where I can't help but think *there has to be a better way*... If you take Mr. Johnson's GetWave method above and combine it with something like this...

            	public static void PlaySoundMemory(string resid)
            	{
            		// Stop current sound
            		PlaySound(
            			IntPtr.Zero,
            			IntPtr.Zero,
            			PlaySoundFlags.SND\_SYNC
            			);
            		// Play sound
            		byte\[\] buffer = GetWave(resid);
            		if (null != buffer)
            		{
            			IntPtr p = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
            			PlaySound(
            				p,
            				IntPtr.Zero,
            				PlaySoundFlags.SND\_SYNC | PlaySoundFlags.SND\_MEMORY
            				);
            		}
            	}
            

            You end up with something that works. You could also write the resource to a temp file and play it directly from there. The PlaySound Function and the PlaySoundFlags are just API definitions lifted from the Platform SDK headers. Regards

            S Offline
            S Offline
            Szymon Pusz
            wrote on last edited by
            #5

            Yesss, now it works!!! Thanks a lot!!!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups