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

TdhSoundFactory - A class to provide sound effects to .NET applications

, 24 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes a class to asynchronously play sound effects in .NET applications. The sounds may derive from embedded resources, from byte arrays, or from files read from disk.

Screenshot - TdhSoundFactory.gif

Introduction

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.

The 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:

  1. Playing the predefined (named) sounds
  2. Toggling sound playback on and off
  3. Playing sounds even when playback has been toggled off
  4. Playing non-predefined sounds from embedded resources
  5. 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

To use TdhSoundFactory as is, add a reference in your project to the class library 'TDHSoundFactory.dll'. The namespace used in this library is:

using TDHControls.TDHSoundFactory;

The 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 'false'. The OverruleSilent argument of the various methods allows for playing a sound even when this value is 'true'.
  • 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 wavBytes.

Wish List

  • Make 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.

History

  • 2007 October 24: Submission of TdhSoundFactory ver. 1.0.001 to The Code Project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ilíon

United States United States
No Biography provided

Comments and Discussions

 
GeneralSimiltaneous Playback Pinmemberzrk29-Oct-07 21:23 
GeneralRe: Similtaneous Playback [modified] PinmemberIlíon30-Oct-07 3:51 
GeneralRe: You can using another API Pinmember98 Z2830-Oct-07 16:10 
GeneralRe: You can using another API PinmemberIlíon30-Oct-07 17:50 
GeneralRe: Similtaneous Playback PinmemberIlíon16-Nov-07 21:22 

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
Web04 | 2.8.141216.1 | Last Updated 24 Oct 2007
Article Copyright 2007 by Ilíon
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid