Click here to Skip to main content
15,176,561 members
Articles / Programming Languages / C#
Posted 13 Jun 2006


21 bookmarked

Wrapping up Managed DirectX AudioVideoPlayback

Rate me:
Please Sign up or sign in to vote.
4.26/5 (6 votes)
13 Jun 2006GPL33 min read
A Wrapper DLL for the AudioVideoPlayback Managed DirectX class
Sample Image - AudioDX.jpg


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.


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;


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.Stop(); //if and when you need to stop the sound.


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


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!


  • 13th June, 2006: Initial post


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Software Developer Operon Biotechnologies
United States United States
My brain isn't the only part of my anatomy from which I pull ideas.

Comments and Discussions

GeneralSaving textures to various formats.... Pin
ap_wooka27-Mar-08 12:48
Memberap_wooka27-Mar-08 12:48 
GeneralRe: Saving textures to various formats.... Pin
ipslor313-Dec-09 4:04
Memberipslor313-Dec-09 4:04 
GeneralRe: Saving textures to various formats.... Pin
ap_wooka21-Jan-10 10:51
Memberap_wooka21-Jan-10 10:51 
GeneralTexture display Pin
sasko00716-Oct-07 11:47
Membersasko00716-Oct-07 11:47 
GeneralRe: Texture display Pin
ap_wooka17-Oct-07 10:04
Memberap_wooka17-Oct-07 10:04 
GeneralRe: Texture display Pin
sasko00718-Oct-07 5:12
Membersasko00718-Oct-07 5:12 
GeneralRe: Texture display Pin
ap_wooka20-Mar-08 9:27
Memberap_wooka20-Mar-08 9:27 
GeneralRe: Texture display Pin
cgimda25-Jun-09 10:03
Membercgimda25-Jun-09 10:03 
GeneralRe: Texture display Pin
ap_wooka25-Jun-09 11:10
Memberap_wooka25-Jun-09 11:10 
GeneralRe: Texture display Pin
cgimda25-Jun-09 11:42
Membercgimda25-Jun-09 11:42 
GeneralRe: Texture display Pin
ap_wooka26-Jun-09 8:59
Memberap_wooka26-Jun-09 8:59 
GeneralIssue with the DLL Pin
ap_wooka6-Sep-07 6:44
Memberap_wooka6-Sep-07 6:44 
GeneralRe: Issue with the DLL Pin
viruswatts15-Oct-07 8:59
Memberviruswatts15-Oct-07 8:59 
GeneralRe: Issue with the DLL Pin
ap_wooka16-Jan-08 11:09
Memberap_wooka16-Jan-08 11:09 
GeneralThank you! Pin
Leslie Sanford21-Jul-07 22:30
MemberLeslie Sanford21-Jul-07 22:30 
GeneralRe: Thank you! Pin
ap_wooka6-Sep-07 6:36
Memberap_wooka6-Sep-07 6:36 
GeneralAwesome Pin
thund3rstruck28-Mar-07 14:39
Memberthund3rstruck28-Mar-07 14:39 
GeneralRe: Awesome Pin
ap_wooka2-Apr-07 9:46
Memberap_wooka2-Apr-07 9:46 
GeneralRe: Awesome Pin
thund3rstruck2-Apr-07 16:00
Memberthund3rstruck2-Apr-07 16:00 
QuestionRemoved from MDX 2.0? Pin
Cale Hoopes13-Jun-06 13:15
MemberCale Hoopes13-Jun-06 13:15 
AnswerRe: Removed from MDX 2.0? Pin
ap_wooka13-Jun-06 21:30
Memberap_wooka13-Jun-06 21:30 
AnswerRe: Removed from MDX 2.0? [modified] Pin
ap_wooka14-Jun-06 4:09
Memberap_wooka14-Jun-06 4:09 
GeneralRe: Removed from MDX 2.0? Pin
Cale Hoopes14-Jun-06 5:51
MemberCale Hoopes14-Jun-06 5:51 

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.