![]() |
Multimedia »
Audio and Video »
Audio
Intermediate
A simple class to allow you to play Wavs in C#By MeterManThe 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#.NET 1.0, Win2K, WinXP, Win2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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.
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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Apr 2004 Editor: Smitha Vijayan |
Copyright 2004 by MeterMan Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |