Click here to Skip to main content
15,919,178 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
2.89/5 (24 votes)
14 Apr 20041 min read 112.9K   1.8K   35   18
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

Introduction

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.

C#
using tonysound;

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

C#
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:

C#
Sound.Play("sound.wav",PlaySoundFlags.SND_FILENAME);

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:

C#
Sound.Play(null,null);

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:

C#
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.

License

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


Written By
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 Pin
Basma.a2-Jan-09 21:12
Basma.a2-Jan-09 21:12 
GeneralHELP Pin
sabsabsab1-Jan-08 8:09
sabsabsab1-Jan-08 8:09 
GeneralAlternative - using sound recorder to play wave file programmatically Pin
daluu19-Jul-07 14:46
daluu19-Jul-07 14:46 
GeneralUse of tony sound library fails to work after compiling Pin
daluu19-Jul-07 14:23
daluu19-Jul-07 14:23 
GeneralDoes not play multiple sounds simultaneously Pin
Jeffrey A Voigt3-Jan-07 2:53
Jeffrey A Voigt3-Jan-07 2:53 
GeneralOther than .wav files Pin
J.S.Lim1-Jul-06 2:32
J.S.Lim1-Jul-06 2:32 
GeneralThis sample only plays a unique sound Pin
Célio23-Jun-06 3:07
Célio23-Jun-06 3:07 
GeneralWaveform audio interface component for .NET Pin
AdamSlosarski4-Nov-05 1:25
AdamSlosarski4-Nov-05 1:25 
QuestionHow about Record Audio? Pin
SuwitLam17-Aug-05 6:54
SuwitLam17-Aug-05 6:54 
AnswerRe: How about Record Audio? Pin
Vash1027-Aug-05 22:49
Vash1027-Aug-05 22:49 
AnswerRe: How about Record Audio? Pin
SuwitLam28-Aug-05 7:15
SuwitLam28-Aug-05 7:15 
GeneralRe: How about Record Audio? Pin
Lwin Naing Oo10-Sep-05 17:23
Lwin Naing Oo10-Sep-05 17:23 
QuestionPlease tell me what do these flags? Where do you find information about them? Pin
liubomir4-Jun-05 19:38
liubomir4-Jun-05 19:38 
GeneralProcedure of using winmm.dll Pin
muddasar mehmood26-Oct-04 18:53
muddasar mehmood26-Oct-04 18:53 
GeneralWaveform audio interface component for .NET Pin
AdamSlosarski4-Nov-05 1:25
AdamSlosarski4-Nov-05 1:25 
GeneralConsider these changes Pin
Mark Pitman15-Apr-04 10:19
Mark Pitman15-Apr-04 10:19 
I took a look at your code and I have some suggestions. I rewrote your tonysound.cs class like so:
<br />
using System;<br />
using System.Runtime.InteropServices;<br />
<br />
namespace tonysound {<br />
	public class Sound {<br />
		public static void Play( string strFileName, PlaySoundFlags soundFlags) {<br />
			PlaySound( strFileName, IntPtr.Zero, soundFlags);<br />
		    <br />
		}<br />
<br />
		[DllImport("winmm.dll")] //inports the winmm.dll used for sound<br />
		private static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );<br />
	}<br />
<br />
	[Flags] //enumeration treated as a bit field or set of flags<br />
	public enum PlaySoundFlags: int {<br />
<br />
		SND_SYNC = 0x0000, /* play synchronously (default) */<br />
		SND_ASYNC = 0x0001, /* play asynchronously */<br />
		SND_NODEFAULT = 0x0002, /* silence (!default) if sound notfound */<br />
		SND_LOOP = 0x0008, /* loop the sound until nextsndPlaySound */<br />
		SND_NOSTOP = 0x0010, /* don't stop any currently playingsound */<br />
		SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */<br />
		SND_FILENAME = 0x00020000, /* name is file name */<br />
		SND_RESOURCE = 0x00040004 /* name is resource name or atom */		<br />
	} <br />
}<br />


then to call the Play method you would do something like this:

Sound.Play("sound.wav", PlaySoundFlags.SND_FILENAME | PlaySoundFlags.SND_ASYNC | PlaySoundFlags.SND_LOOP | PlaySoundFlags.SND_NOSTOP);

There are a few reasons to implement these changes. You don't have to know what strings to pass in to the Play method, you don't have to pass any more flags in than you actually need, and you don't have to do string comparisons (yuck!).

Mark Pitman
GeneralRe: Consider these changes Pin
MeterMan15-Apr-04 11:38
MeterMan15-Apr-04 11:38 
GeneralTonysound Class Pin
dzzxyz15-Apr-04 8:02
dzzxyz15-Apr-04 8:02 

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.