Don’t underestimate simple APIs

I was playing around with PlaySound today (no pun intended) and was kind of surprised to find that I could play an embedded resource wave file with one line of code (well one line past the P/Invoke declaration):


using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;


….


// P/Invoke declarations….
[DllImport(“CoreDll.DLL”, EntryPoint=”PlaySound”, SetLastError=true)]
private extern static int PlaySound(byte
[] szSound, IntPtr hMod, SoundFlags flags);


[Flags]
enum SoundFlags
{
  Alias = 0x00010000,
  Filename = 0x00020000,
  Synchronous = 0x00000000,
  Asynchronous = 0x00000001,
  Memory = 0x00000004,
  Loop = 0x00000008,
  NoStop = 0x00000010
}



PlaySound(((MemoryStream)Assembly.GetExecutingAssembly(
  ).GetManifestResourceStream(
  “AudioTest.tada.wav”)).GetBuffer(),
  IntPtr.Zero,
  SoundFlags.Synchronous | SoundFlags.Memory);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s