Click here to Skip to main content
Click here to Skip to main content

A simple class to allow you to play Wavs in C#

, 14 Apr 2004
Rate this:
Please Sign up or sign in to vote.
The framework, from what I have seen, does not include a function to play sound. So, after looking around and gathering information on how to do such, I ended up making a class. I like to use this for error messages. I simply imported the DLL file winmm.dll and used the PlaySound function from it.

Sample Image - screenshot.jpg


The tonysound.cs class gives you the ability to add Wav sound to your Windows forms. Simply include the tonysound.cs in your project folder. This is my first article, so please don't beat me up too bad.

Including the code in your form

Add the tonysound.cs file to your project. Then at the top of the calling form, for example, form1, type the following line to use the tonysound class.

using tonysound;

Once using tonysound; is included, then simply play the sound by calling:

Sound.Play(stringOfFilename,PlaySoundFlags.selectaflaghere );

Note: You can pass as many flags as you want, but if you're playing a file then you need to do something like I have in my code:


The first parameter of Sound.Play is the filename. The filename can be the actual path, for example c:\\sound.wav, or it can simply be a Wav file that is in the same directory as the exe. The second is the flag for playing the sound. You can play a null sound to turn off the sound as follows:


NOTE: You must include either PlaySoundFlags.SND_FILENAME or PlaySoundFlags.SND_RESOURCE in order to actually play the song. The remaining flags are optional.

List of PlaySoundFlags and their use:

SND_SYNC //plays the sound after the event for example a button being pushed
SND_ASYNC //play sound as soon as event happens 
SND_NODEFAULT //don't play the default sound if the soundsource is not found 
SND_LOOP //play the wav over and over 
SND_NOSTOP //don't stop any currently playing sound 
SND_NOWAIT //don't wait if the sound driver is busy
SND_RESOURCE //indicates you are using a wav from a resource
SND_FILENAME //indicates you are using a wav from a file source

These are changes to my previous post, thanks to the suggestions.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author


United States United States
Graduated from Kennesaw State with a Computer Informatin Systems degree. I have a fair knowledge of C++ and C# but by far am not an expert. My skills are more into software troubleshooting and repair.
Currently working as an IT Specialist for a Radio Read Meter Company.

Comments and Discussions

QuestionHow to play mp3 file instead of wav file PinmemberBasma.a2-Jan-09 22:12 
GeneralHELP Pinmembersabsabsab1-Jan-08 9:09 
GeneralAlternative - using sound recorder to play wave file programmatically Pinmemberdaluu19-Jul-07 15:46 
GeneralUse of tony sound library fails to work after compiling Pinmemberdaluu19-Jul-07 15:23 
Has anybody encountered this? I'll probably have to try compiling on other computers to see what happens.
I made use of tony sound library per instructions in the article. After compilation, it fails to play wave file. I get silence. It's even worse on console applications, it hangs trying to play the wave file.
I originally used it for a console application (built with SharpDevelop 2.0). When that didn't work, I tried the same thing on Visual Studio 2003. When that didn't work, tried builing a GUI app on Visual Studio 2003. When that didn't work, tried using the provided demo application and source files to test.
The demo ran fine. The source ran fine after compiling. Modified the source to play a different wave file, and that worked too.
My system is running Windows XP SP2, with .NET v1.1, v2.0, Visual Studio 2003, SharpDevelop 2.0. I verified I have winmm.dll on my system as well.
My code is roughly the same as the provided demo, I have no idea what the heck's going on. I wish the compile would fail with an message or that the running the app would give a runtime error. Too bad all it does is "nothing" or hang. Guess I'll try analyzing with a debugger when I have some time.
btw, be nice if someone could compile a console app that takes an argument for the path to a wave file to play. That's what I was originally trying to do.
"A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible." - Freeman Dyson

GeneralDoes not play multiple sounds simultaneously Pinmemberjvelite3-Jan-07 3:53 
GeneralOther than .wav files PinmemberJ.S.Lim1-Jul-06 3:32 
GeneralThis sample only plays a unique sound PinmemberCélio23-Jun-06 4:07 
GeneralWaveform audio interface component for .NET PinmemberAdamSlosarski4-Nov-05 2:25 
QuestionHow about Record Audio? PinmemberSuwitLam17-Aug-05 7:54 
AnswerRe: How about Record Audio? PinmemberVash1027-Aug-05 23:49 

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
Web03 | 2.8.150327.1 | Last Updated 15 Apr 2004
Article Copyright 2004 by MeterMan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid