How to play WAVE sound from resource?
-
I put a WAVE file into the resource as a Embedded resource. How to play it now? Could someone give me an example?
-
I put a WAVE file into the resource as a Embedded resource. How to play it now? Could someone give me an example?
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!
-
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!
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?
-
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?
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
-
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
Yesss, now it works!!! Thanks a lot!!!