Wrapping up Managed DirectX AudioVideoPlayback
A Wrapper DLL for the AudioVideoPlayback Managed DirectX class

Introduction
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 RenderToTexture()
method.
Background
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 using
statement.
using AudioDX;
Sound
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 Play()
function.
Prototype: public Sound( IntPtr Handle, string Filename )
Sound m_click = new Sound( this.Handle, "gbClick.wav" );
m_click.Play();
m_click.Stop(); //if and when you need to stop the sound.
Music
Much like the Sound
class, create a new instance of the Music
class and call its Play()
function. Alternatively, you can set AutoPlay
to 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.
Prototype: public Music( string FileName,bool AutoRun )
Music m_song = new Music( "Openning.wav", false );
m_song.Play();
m_song.Pause(); //when you need to pause the song.
m_song.Stop(); //when you need to stop the song.
m_song.Load( "NewSong.wav" ); //Open a new song without creating a new instance.
Video
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.
Prototype: public Movie( IntPtr Handle, Rectangle Bounds, string FileName )
Movie m_movie;
m_movie = new Movie( this.Handle, this.Bounds, "ws_mcchris.wmv" );
m_movie.Play(); //Stop(), Pause().
//Work around for Access Violation Exception
m_video.Disposing += new EventHandler( m_video_Disposing );
void m_video_Disposing( object sender, EventArgs e )
{
while( true );
}//Video Disposing
Points of Interest
The 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 Sound
and 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!
History
- 13th June, 2006: Initial post