Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

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 90K   1.2K   22   23
A Wrapper DLL for the AudioVideoPlayback Managed DirectX class
Sample Image - AudioDX.jpg

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.

C#
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 )

C#
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 )

C#
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 )

C#
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

License

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


Written By
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 11:48
ap_wooka27-Mar-08 11:48 
GeneralRe: Saving textures to various formats.... Pin
ipslor313-Dec-09 3:04
ipslor313-Dec-09 3:04 
GeneralRe: Saving textures to various formats.... Pin
ap_wooka21-Jan-10 9:51
ap_wooka21-Jan-10 9:51 
GeneralTexture display Pin
sasko00716-Oct-07 10:47
sasko00716-Oct-07 10:47 
GeneralRe: Texture display Pin
ap_wooka17-Oct-07 9:04
ap_wooka17-Oct-07 9:04 
GeneralRe: Texture display Pin
sasko00718-Oct-07 4:12
sasko00718-Oct-07 4:12 
GeneralRe: Texture display Pin
ap_wooka20-Mar-08 8:27
ap_wooka20-Mar-08 8:27 
GeneralRe: Texture display Pin
cgimda25-Jun-09 9:03
cgimda25-Jun-09 9:03 
GeneralRe: Texture display Pin
ap_wooka25-Jun-09 10:10
ap_wooka25-Jun-09 10:10 
GeneralRe: Texture display Pin
cgimda25-Jun-09 10:42
cgimda25-Jun-09 10:42 
GeneralRe: Texture display Pin
ap_wooka26-Jun-09 7:59
ap_wooka26-Jun-09 7:59 
GeneralIssue with the DLL Pin
ap_wooka6-Sep-07 5:44
ap_wooka6-Sep-07 5:44 
GeneralRe: Issue with the DLL Pin
viruswatts15-Oct-07 7:59
viruswatts15-Oct-07 7:59 
GeneralRe: Issue with the DLL Pin
ap_wooka16-Jan-08 10:09
ap_wooka16-Jan-08 10:09 
I have found if you build the application prior to running it in debug mode the hang doesn't occur nearly as much.

Hope this helps a bit
GeneralThank you! Pin
Leslie Sanford21-Jul-07 21:30
Leslie Sanford21-Jul-07 21:30 
GeneralRe: Thank you! Pin
ap_wooka6-Sep-07 5:36
ap_wooka6-Sep-07 5:36 
GeneralAwesome Pin
thund3rstruck28-Mar-07 13:39
thund3rstruck28-Mar-07 13:39 
GeneralRe: Awesome Pin
ap_wooka2-Apr-07 8:46
ap_wooka2-Apr-07 8:46 
GeneralRe: Awesome Pin
thund3rstruck2-Apr-07 15:00
thund3rstruck2-Apr-07 15:00 
QuestionRemoved from MDX 2.0? Pin
Cale Hoopes13-Jun-06 12:15
Cale Hoopes13-Jun-06 12:15 
AnswerRe: Removed from MDX 2.0? Pin
ap_wooka13-Jun-06 20:30
ap_wooka13-Jun-06 20:30 
AnswerRe: Removed from MDX 2.0? [modified] Pin
ap_wooka14-Jun-06 3:09
ap_wooka14-Jun-06 3:09 
GeneralRe: Removed from MDX 2.0? Pin
Cale Hoopes14-Jun-06 4:51
Cale Hoopes14-Jun-06 4: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.