Click here to Skip to main content
Click here to Skip to main content
Technical Blog

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

, 30 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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!

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

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

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


FileInfo fi = new FileInfo(sFile);


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


About the Author

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

Comments and Discussions

QuestionCompile errors in implementing this solution PinmemberAaditya200611-Jul-12 19:52 
AnswerRe: Compile errors in implementing this solution Pinmembernewkie12-Jul-12 1:00 
GeneralRe: Compile errors in implementing this solution PinmemberMember 904013713-Sep-12 7:06 
GeneralRe: Compile errors in implementing this solution PinmemberMember 904013713-Sep-12 7:37 
GeneralRe: Compile errors in implementing this solution PinprofessionalFrank T. Clark8-May-13 5:03 
GeneralNice info Pinmemberlukilooks30-Mar-11 20:04 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150302.1 | Last Updated 30 Mar 2011
Article Copyright 2011 by Chris_Green
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid