![]() |
Multimedia »
Audio and Video »
Audio
Intermediate
Sound Effects Rack using Windows Media PlayerBy Profox JaseA simple pair of classes that provide multiple wav file playing in C# |
C#, Windows, .NET 1.0, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
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.
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:
SoundModuleThis 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.
SoundModule()Constructor. Does loads of nasty stuff I don't really understand. There are notes in the code on how to cause problems.
isReadyis 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.
playStateChange()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.
PlayFile()duh.......
EffectsRackConceptually 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.
AddModule()
examines the array of sound modules, extends the array if necessary, then adds a new module in the appropriate place in the array.
PlayEffect()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.
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 30 Mar 2002 Editor: Nishant Sivakumar |
Copyright 2002 by Profox Jase Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |