5,427,303 members and growing! (19,632 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Audio     Intermediate

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

By MeterMan

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.
C#Windows, .NET, .NET 1.0, Win2K, WinXP, Win2003VS.NET2002, Visual Studio, Dev

Posted: 14 Apr 2004
Updated: 14 Apr 2004
Views: 61,079
Bookmarked: 23 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
24 votes for this Article.
Popularity: 3.98 Rating: 2.89 out of 5
8 votes, 33.3%
1
2 votes, 8.3%
2
1 vote, 4.2%
3
4 votes, 16.7%
4
9 votes, 37.5%
5

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.

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:

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:

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:

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

About the Author

MeterMan


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.
Location: United States United States

Other popular Audio and Video articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralHELPmembersabsabsab9:09 1 Jan '08  
GeneralAlternative - using sound recorder to play wave file programmaticallymemberdaluu15:46 19 Jul '07  
GeneralUse of tony sound library fails to work after compilingmemberdaluu15:23 19 Jul '07  
GeneralDoes not play multiple sounds simultaneouslymemberjvelite3:53 3 Jan '07  
GeneralOther than .wav filesmemberJ.S.Lim3:32 1 Jul '06  
GeneralThis sample only plays a unique soundmemberCélio4:07 23 Jun '06  
GeneralWaveform audio interface component for .NETmemberAdamSlosarski2:25 4 Nov '05  
GeneralHow about Record Audio?memberSuwitLam7:54 17 Aug '05  
QuestionRe: How about Record Audio?memberVash1023:49 27 Aug '05  
AnswerRe: How about Record Audio?memberSuwitLam8:15 28 Aug '05  
GeneralRe: How about Record Audio?memberLwin Naing Oo18:23 10 Sep '05  
GeneralPlease tell me what do these flags? Where do you find information about them?memberliubomir20:38 4 Jun '05  
GeneralProcedure of using winmm.dllmemberMohdYounisKhan19:53 26 Oct '04  
GeneralWaveform audio interface component for .NETmemberAdamSlosarski2:25 4 Nov '05  
GeneralConsider these changesmemberMark Pitman11:19 15 Apr '04  
GeneralRe: Consider these changesmemberwin32newb12:38 15 Apr '04  
GeneralTonysound ClassmemberDaniel Stephen Rule9:02 15 Apr '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Apr 2004
Editor: Smitha Vijayan
Copyright 2004 by MeterMan
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project