This code is a tight little library encasing the managed DirectX
AudioVideoPlayback class. It is broken into three sections, Sounds, Music and Videos. I wrote this code to simplify the process of playing multiple sounds at once, while playing background music. While I was at it, I figured I would add the Video portion just to round the class out. The video section proved to be the most challenging, as I wanted to embed the player as opposed to it opening its own window. I did a lot of searching and found many people asking a lot of the same questions, but no real answers. I also came up against the inherent issues that surround the infamous
Managed DirectX SDK should be installed in order to use this library. However you don't have to know much, or anything at all really about DirectX to get it to work.
Using the Code
The first thing you will need to do is add the AudioDX.dll to your references and add your
We will start with the sound portion of the library. Simply create a new instance of the
Sound class, either globally or locally. Once loaded, it can be played simply by calling its
public Sound( IntPtr Handle, string Filename )
Sound m_click = new Sound( this.Handle, "gbClick.wav" );
Much like the
Sound class, create a new instance of the
Music class and call its
Play() function. Alternatively, you can set
true and it will simply begin as soon as it has loaded. So far this class has worked for every audio file type I have tried it with.
public Music( string FileName,bool AutoRun )
Music m_song = new Music( "Openning.wav", false );
m_song.Load( "NewSong.wav" );
Now things start getting interesting. Using the
RenderToTexture() function under certain circumstances can cause numerous errors when attempting to dispose of the object. Most commonly, an Access Violation Exception is thrown. Having done my research on this, I have learned that this is a bug they are aware of but have no intention of fixing. I fumbled upon a work around for this quite by accident. If you handle the
Disposing event of the
Video object and stall it indefinitely, it allows the program to complete shutting down without throwing the error. This is not something I usually recommend, but it seems to do the trick. I found this when I added a
MessageBox to the disposing event handler. The program closed without error before I was able to respond to the
MessageBox. This workaround is already incorporated into the library, but for all of you who have been having this problem, I hope this works for you.
public Movie( IntPtr Handle, Rectangle Bounds, string FileName )
m_movie = new Movie( this.Handle, this.Bounds, "ws_mcchris.wmv" );
m_video.Disposing += new EventHandler( m_video_Disposing );
void m_video_Disposing( object sender, EventArgs e )
while( true );
Points of Interest
Movie class, while not my original focus, became the real gem of this library. I did not approach it in the typical manner but am happy with the results. There really isn't much to the
Music classes, however bundled all together these three elements form the very basis for all of your
AudioVideoPlayback needs. I stopped developing the library once it met my requirements, but there is a lot more functionality that could be added to really up the versatility of this library. I'll leave that bit up to you guys. Thanks and enjoy!
- 13th June, 2006: Initial post