This project originated with a small section of code from (an older version of) Calum McLellan's article: Explorer ComboBox and ListView in VB.NET. I translated his routine for using "winmm.dll" to play sound files into C#, and later expanded upon that.
TdhSoundFactory class has four predefined or named sounds (which are embedded resources), and allows for one "dynamic" sound (i.e., assignable at run-time). The class may play sounds which derive from embedded resources, from byte arrays passed as an argument, or from files read from disk.
The demo example demonstrates:
- Playing the predefined (named) sounds
- Toggling sound playback on and off
- Playing sounds even when playback has been toggled off
- Playing non-predefined sounds from embedded resources
- Playing a sound loaded from a file on disk
The sounds played by the "Horse Snort" and "Oink" buttons are embedded resources (as the class stands), which you'll probably want to remove to make the compiled library a bit smaller. They are "Easter eggs" for a couple of my co-workers.
And, of course, you may well want to replace any of the predefined/named sounds and/or expand the number of named sounds.
Using the Class
TdhSoundFactory as is, add a reference in your project to the class library 'TDHSoundFactory.dll'. The namespace used in this library is:
TdhSoundFactory class was written (and compiled) using VS2002 (.NET 1.0) with the intention that the source code be readily available to other developers regardless of the .NET version they are using.
The members of
TdhSoundFactory's interface are:
Silent - This boolean property determines whether sound playback is toggled on or off. The default value is '
OverruleSilent argument of the various methods allows for playing a sound even when this value is '
Blip(bool OverruleSilent) - This method plays one of the predefined sounds from the embedded resources.
Click(bool OverruleSilent) - This method plays one of the predefined sounds from the embedded resources.
Oops(bool OverruleSilent) - This method plays one of the predefined sounds from the embedded resources.
Question(bool OverruleSilent) - This method plays one of the predefined sounds from the embedded resources.
Play(string wavName, bool RetainAfterPlaying, bool OverruleSilent) - This method plays a sound-clip either from a non-predefined embedded resource, or from a file read from disk. The method first tries to find an embedded resource named with the value passed in
wavName; if that fails, it assumes the value is the path to a file on disk; if the value cannot be resolved as a valid path, the method does nothing. The
RetainAfterPlaying value indicates whether the sound-clip should be retained in memory after being played. This avoids having to re-read the file each time it is to be played.
Play(byte wavBytes, bool OverruleSilent) - This method plays a sound-clip contained in the byte array
TdhSoundFactory more flexible -- Currently, the class has four predefined sounds, and allows for one "dynamic" sound (i.e., assignable at run-time). I intend to allow for more sound-clips to be assignable at run-time.
- 2007 October 24: Submission of
TdhSoundFactory ver. 1.0.001 to The Code Project.