Click here to Skip to main content
Click here to Skip to main content
Go to top

Wrapping up Managed DirectX AudioVideoPlayback

, 13 Jun 2006
Rate this:
Please Sign up or sign in to vote.
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.

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

License

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

Share

About the Author

ap_wooka
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.... Pinmemberap_wooka27-Mar-08 11:48 
GeneralRe: Saving textures to various formats.... Pinmemberipslor313-Dec-09 3:04 
GeneralRe: Saving textures to various formats.... Pinmemberap_wooka21-Jan-10 9:51 
GeneralTexture display Pinmembersasko00716-Oct-07 10:47 
GeneralRe: Texture display Pinmemberap_wooka17-Oct-07 9:04 
GeneralRe: Texture display Pinmembersasko00718-Oct-07 4:12 
GeneralRe: Texture display Pinmemberap_wooka20-Mar-08 8:27 
GeneralRe: Texture display Pinmembercgimda25-Jun-09 9:03 
GeneralRe: Texture display Pinmemberap_wooka25-Jun-09 10:10 
GeneralRe: Texture display Pinmembercgimda25-Jun-09 10:42 
GeneralRe: Texture display Pinmemberap_wooka26-Jun-09 7:59 
GeneralIssue with the DLL Pinmemberap_wooka6-Sep-07 5:44 
GeneralRe: Issue with the DLL Pinmemberviruswatts15-Oct-07 7:59 
GeneralRe: Issue with the DLL Pinmemberap_wooka16-Jan-08 10:09 
GeneralThank you! PinmemberLeslie Sanford21-Jul-07 21:30 
GeneralRe: Thank you! Pinmemberap_wooka6-Sep-07 5:36 
GeneralAwesome Pinmemberthund3rstruck28-Mar-07 13:39 
GeneralRe: Awesome Pinmemberap_wooka2-Apr-07 8:46 
GeneralRe: Awesome Pinmemberthund3rstruck2-Apr-07 15:00 
QuestionRemoved from MDX 2.0? PinmemberCale Hoopes13-Jun-06 12:15 
AnswerRe: Removed from MDX 2.0? Pinmemberap_wooka13-Jun-06 20:30 
AnswerRe: Removed from MDX 2.0? [modified] Pinmemberap_wooka14-Jun-06 3:09 
GeneralRe: Removed from MDX 2.0? PinmemberCale Hoopes14-Jun-06 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 13 Jun 2006
Article Copyright 2006 by ap_wooka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid