Click here to Skip to main content
15,891,375 members
Articles / Programming Languages / C#

PlaySound: A Better Way to Play Wav Files in C#

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
30 Mar 2011CPOL 35K   2   7
Using PlaySound with PInvoke.

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!

C#
public static class Wav
{
    [DllImport("winmm.dll", SetLastError = true)]
    static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound);

    [Flags]
    public enum SoundFlags
    {
        /// <summary>play synchronously (default)</summary>
        SND_SYNC = 0×0000,
        /// <summary>play asynchronously</summary>
        SND_ASYNC = 0×0001,
        /// <summary>silence (!default) if sound not found</summary>
        SND_NODEFAULT = 0×0002,
        /// <summary>pszSound points to a memory file</summary>
        SND_MEMORY = 0×0004,
        /// <summary>loop the sound until next sndPlaySound</summary>
        SND_LOOP = 0×0008,
        /// <summary>don’t stop any currently playing sound</summary>
        SND_NOSTOP = 0×0010,
        /// <summary>Stop Playing Wave</summary>
        SND_PURGE = 0×40,
        /// <summary>don’t wait if the driver is busy</summary>
        SND_NOWAIT = 0×00002000,
        /// <summary>name is a registry alias</summary>
        SND_ALIAS = 0×00010000,
        /// <summary>alias is a predefined id</summary>
        SND_ALIAS_ID = 0×00110000,
        /// <summary>name is file name</summary>
        SND_FILENAME = 0×00020000,
        /// <summary>name is resource name or atom</summary>
        SND_RESOURCE = 0×00040004
    }

    public static void Play(string strFileName)
    {
        PlaySound(strFileName, UIntPtr.Zero, 
           (uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC));
    }
}

Example:

C#
FileInfo fi = new FileInfo(sFile);
Wav.Play(fi.FullName);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Avaya Inc.
Ireland Ireland
Formerly a C++ client developer, nowadays I'm all about C# and ASP.NET. Over the years I have mastered some and played with many aspects of .NET.

Follow my blog as I catalogue the more arcane problems I encounter and their solutions at CodingLifestyle.com

Comments and Discussions

 
QuestionMSDN article with same info Pin
TheProgrammer23116-Apr-16 7:33
TheProgrammer23116-Apr-16 7:33 
QuestionCompile errors in implementing this solution Pin
UpendraWatwe11-Jul-12 18:52
UpendraWatwe11-Jul-12 18:52 
I tried to implement your solution, but could not understand how to resolve the compile time error resulting from the x in values set to the flags.

I have a windows server 2008 R2 DataCenter edition with SP1 (in process of being installed as my earlier attempt involved using System.Media soundplayer about which it was given in MSDN that server core is not supported for above OS but with server core with SP1 is supported.
Can you help me out in this - I may be missing something very basic.
Upendra Watwe
Aaditya Infotech
+91-9822976099
upendra.watwe@aadityainfotech.com
upendra.watwe@gmail.com

AnswerRe: Compile errors in implementing this solution Pin
Chris_Green12-Jul-12 0:00
Chris_Green12-Jul-12 0:00 
GeneralRe: Compile errors in implementing this solution Pin
B. Clay Shannon13-Sep-12 6:06
professionalB. Clay Shannon13-Sep-12 6:06 
GeneralRe: Compile errors in implementing this solution Pin
B. Clay Shannon13-Sep-12 6:37
professionalB. Clay Shannon13-Sep-12 6:37 
GeneralRe: Compile errors in implementing this solution Pin
Frank T. Clark8-May-13 4:03
professionalFrank T. Clark8-May-13 4:03 
GeneralNice info Pin
lukilooks30-Mar-11 19:04
lukilooks30-Mar-11 19:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.