Click here to Skip to main content
15,569,644 members
Articles / Programming Languages / C#
Posted 30 Mar 2002


36 bookmarked

Sound Effects Rack using Windows Media Player

Rate me:
Please Sign up or sign in to vote.
3.92/5 (4 votes)
30 Mar 20024 min read
A simple pair of classes that provide multiple wav file playing in C#


Further to prototyping an array of Windows Media Players in my Asteroids game, this here is a refined version - the downloadable files consist of all you should need play around with my code, including two sample sound files (copyright unknown - I have not produced these files myself, and it would be unwise to use them in a commercial app). An improvement on the previous version is that I no longer need to throw the media player into error in order to release a file.

Why write these two classes?

Until the great MS release DirectX 9 for .net or someone else shows me another way, I cannot think of another way to play more than one sound at once, and in the case of my asteroids game, play several instances of any particular sound (for example, 3 explosions at once, all based the same wav file).

As usual, if you use my code anywhere for work, please give me a mention somewhere.

There are three classes included in the sample:

  • Form1

    - the ever exciting default form plus 3 buttons for pushing pleasure.
  • SoundModule

    - a wrapper for the ActiveX Windows Media Player.
  • EffectsRack

    - a usercontrol briefly consisting of an array of SoundModules. Heh heh, could have used a more descriptive name, but today I am batting for the team with the exciting groovy names.

Using the sample

  • Download the code (natch) and unzip it to an appropriate folder.
  • Create a d:\temp folder or equivalent, then alter the code accordingly (private string tempFolder = "d:\\temp\\"; in EffectsRack class).
  • Run the form - when it starts, it plays one of the sounds.
  • Push each button in turn until you get bored. No prizes for working out what they do, though you can send me currency if you wish.


The two exciting classes are an example of loose coupling (by my understanding) - that is EffectsRack relies on the soundModule class, although the soundModule class may be used without an EffectsRack (but be warned, currently the soundModule class assumes that file to be played is disposable - it will be deleted after play).


This baby is a wrapper for the Windows Media Player, which in turn is wrapped the IDE using the ActiveX Import tool - a few details can be found at this Microsoft page.

My understanding is that the importer looks at an OCX and constructs a wrapper class around it that conforms to C# / IL standards. I don't really understand how the importer works and my knowledge of the wrapper generated is negligible. Answers on a postcard to my usual email address please.


Constructor. Does loads of nasty stuff I don't really understand. There are notes in the code on how to cause problems.


is a flag set by the playing of a file (from True to False) and the cessation of play (False becoming True). It is set to True by the PlayFile method and changes back to False in the playStateChange event delegate.


Subscriber to the appropriate event in the Windows Media Player. This method will update the isReady field and, if the player is done playing will unload the file AND DELETE IT. You have been warned.




Conceptually this a wrapper for an array with a few methods thrown in for joy. Upon instantiation, an effects rack will initialise an array 5 soundModules. You may add more by calling the AddModule method.

Since Windows Media Player does not allow for file sharing, to play the same file many times at once, the EffectsRack will take a target file and copy it to a new filename. The original file is never played or deleted. The new filename is then passed to a soundModule which will play the file and then delete it, thus one of the reasons I have described this pair as loosely coupled.


examines the array of sound modules, extends the array if necessary, then adds a new module in the appropriate place in the array.


Searches the array of modules for one that isReady. If it doesn't find one, it doesn't play the sound. If a module isReady, a new file name is created and the target file copied to it. The new file name is then passed onto the module for playing.

Well, that's about it for another article. For details of how to add the Multimedia control to your C# toolbox, refer to my earlier Asteroids article posting.

As usual, I would love any constructive feedback, and I hope that in some way this code has been useful.


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
Web Developer
United Kingdom United Kingdom
Profox Jase
Co-Author, "Cross-Platform .NET Development: Using Mono, Portable.NET, and Microsoft .NET"
(Apress, 2004, ISBN: 1-59059-330-8)
The Book

Comments and Discussions

GeneralWe face this problem.. plz hlp us Pin
Ibrahim Dwaikat7-Nov-07 1:03
Ibrahim Dwaikat7-Nov-07 1:03 

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.