The other day, I was whipping up a fun utility which played some Wav files. I was giving this to people whose desktop was Windows Server 2008 so using the Windows Media Player COM object wasn’t an option and SoundPlayer didn’t seem to work with any of the Wav files I had for some reason.
Back in my C++ days, I used to do this all the time with winmm.dll’s PlaySound
(and have a piece of freeware which uses this to a great extent).
Well, once again, as a C# programmer, I am saved by PInvoke!
public static class Wav
{
[DllImport("winmm.dll", SetLastError = true)]
static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound);
[Flags]
public enum SoundFlags
{
SND_SYNC = 0×0000,
SND_ASYNC = 0×0001,
SND_NODEFAULT = 0×0002,
SND_MEMORY = 0×0004,
SND_LOOP = 0×0008,
SND_NOSTOP = 0×0010,
SND_PURGE = 0×40,
SND_NOWAIT = 0×00002000,
SND_ALIAS = 0×00010000,
SND_ALIAS_ID = 0×00110000,
SND_FILENAME = 0×00020000,
SND_RESOURCE = 0×00040004
}
public static void Play(string strFileName)
{
PlaySound(strFileName, UIntPtr.Zero,
(uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC));
}
}
Example:
FileInfo fi = new FileInfo(sFile);
Wav.Play(fi.FullName);