Click here to Skip to main content
12,511,383 members (124,934 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

17.4K views
2 bookmarked
Posted

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);

    [Flags]
    public enum SoundFlags
    {
        /// <span class="code-SummaryComment"><summary>play synchronously (default)</summary>
</span>

Example:

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)

Share

About the Author

Chris_Green
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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionMSDN article with same info Pin
TheProgrammer23116-Apr-16 7:33
memberTheProgrammer23116-Apr-16 7:33 
QuestionCompile errors in implementing this solution Pin
Aaditya200611-Jul-12 18:52
memberAaditya200611-Jul-12 18:52 
AnswerRe: Compile errors in implementing this solution Pin
newkie12-Jul-12 0:00
membernewkie12-Jul-12 0:00 
GeneralRe: Compile errors in implementing this solution Pin
Member 904013713-Sep-12 6:06
memberMember 904013713-Sep-12 6:06 
GeneralRe: Compile errors in implementing this solution Pin
Member 904013713-Sep-12 6:37
memberMember 904013713-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
memberlukilooks30-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.

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