Click here to Skip to main content
Click here to Skip to main content

Problems in the AudioVideoPlayback namespace of managed DirectX9

, 13 Sep 2004
Rate this:
Please Sign up or sign in to vote.
A guided tour of the many reasons NOT to use DirectX9 for your audio/video playback needs

Introduction

With DirectX 9, it's finally possible to use DirectX from C# without having to use interop. This includes the AudioVideoPlayback namespace, which as the name would imply, is used to play audio and video files. It comprises largely of two objects, an Audio class, and a Video class (which contains an Audio property). The interface looks pretty simple, and so you'd be forgiven for quickly deciding that writing a video player would be as easy as this:

if (browseForFile)
{
    if (video == null)
        video = new Video(filePath);    
    else
        video.Open(filePath);    
    video.Owner = videoPanel;
    video.Size = videoPanel.Size;
    video.Ending +=new EventHandler(video_Ending);
    video.Play();
}

Unfortunately, almost every line of this code is broken. The point of this article is to explain the pitfalls of the AudioVideoPlayback in DirectX 9, and the ways that I found to work around them. I'll do this line by line, turning the above example into something that reliably works.

The constructor

The first one is the worst one, in a lot of ways. The Video class offers an Open method, it does not offer a parameterless constructor. In other words, you must build a Video object out of a file, and then you have a method whereby you can open other videos with the same object. It leaks memory. Like crazy. In order to reuse a Video object, you need to first delete it, like this:

if (video != null)
{
    video.Stop();
    video.Dispose();
    video = null;
}
video = new Video(filePath);

Yes, you need to force destruction of the Video object, and create a new one. I have no idea why, or how this can be adequately explained (especially given the great fuss that Microsoft made over code now being 'managed'). I certainly urge you not to ever use the Open method.

The owner and the size

Setting the owner of the video is fine, not too much can go wrong, so long as you get it right. I got some really weird behavior during development, but nothing that didn't go away when I sorted out my code. The size is a different story. You can set the size of a video and it will play in that size fine. But once the video ends, the control it is playing on will resize itself to the native resolution of the video. To defeat this, you need to set an OnSize event handler in the control that the video plays on, and manually resize the control to the desired size, which can be stored as a property or calculated for a resizable window, or simply write code that stops a window from ever shrinking if your app is topmost and full screen (so the screen res is never changed, and the app is never resized). In the case of the sample app, it looks like this:

private void videoPanel_SizeChanged(object sender, EventArgs e)
{
    videoPanel.Size.Width = this.Size.Width - 10;
    videoPanel.Size.Height = this.Size.Height - 10;
}

Catching the end of play

When I saw that the Video object has an event handler for the end of a file, I thought that seemed pretty logical and useful. It is, and sometimes, it even gets called. The Audio object event always seems to fire, unless it's the Audio property on a Video object, but video just does not consistently work. So I set up a separate timer, to try and work out for myself what was going on, something like:

private void videoTimer(object sender, EventArgs e)
{
    if (!video.Playing)
        OnVideoEnd(sender, e);
}

Unfortunately, even when a video stops, the Playing property remains 'true'. So instead, I had to do this:

private void videoTimer(object sender, EventArgs e)
{
    if (video.CurrentPosition >= video.Duration)
        OnVideoEnd(sender, e);
}

The CurrentPosition always seems to go past the Duration, even though the Playing property remains true, and the Ending event often does not fire.

How loud is my audio?

The Audio property of a Video contains all the same methods as the Audio class, but they work on the audio contained in a video clip. For my application, I needed to play the clip on the second monitor, and provide a preview on the main monitor. So I decided to play the video twice, and turn the sound off on one of them. The Audio class has a Volume property, an integer. I tried setting this to 0, and nothing seemed to happen. I read some docs, which said that off was 10000, and on is 0, so I tried that and it blew up. Finally, I found another snippet in the MSDN which told me (correctly) that off is -10000. I'm sure there is some esoteric reason why the property is called Volume, and 0 is full on, and -10000 (why not -100000, or -94567547???) is off. Nevertheless, that's how it works.

More than video?

If you want to use a panel to display still images and video, this may work, so long as you destroy the video object first. I had no luck though, the last frame of video always stayed, and I could not draw over it. If you create a custom control which encapsulates bringing forward a Panel for video, or a PictureBox for stills, that won't work either. Putting a PictureBox and a Panel on your main form and calling BringToFront on whichever one you are using works fine though.

Two videos at once?

My next problem was the showstopper. My application runs in two screens, with one screen showing full screen video, and the other showing a thumbnail. The only way I could see to do this was to play two videos. The screen with a thumbnail also has a custom control which shows a list of available media. As soon as this control is used, the main video screen would freeze. A bit of experimentation established that no matter what size your two video objects were, the biggest one freezes when there are paint messages about. The client was not happy.....

A wrapper class?

I fully intended to provide a wrapper class that turns the Video object into something that behaves in a rational manner, so people could use that instead, but then I heard from someone else that they were having trouble playing 320 kbps MP3s, and getting the same callback problems with audio that I had with video. I have tested mostly on video, given that it seems a superset of audio in terms of what it does, so with a week to go before delivery, I gutted my project and replaced DX9 with the Windows Media Player control. Yes, the code is not as pretty, but guess what? Once I worked out the messages it sends me (and sends me every time, may I add), I suddenly found that everything worked for the first time. Not only that, but I can play two big videos with no problems at all. The only trick is to stop the players from autostarting, so both can load their copy of the video before asking both to play it. Unless you have a very specific reason to use DirectX9, I totally recommend you forget it, and play audio and video in C# using the Media Player control. The sample in the SDK gives you everything you need to work it all out, but if a lot of people ask for a tutorial, I'll write one.

Where from here?

The first thing I did when taking on this job was order a book from Amazon on Managed DirectX 9. Do not buy this book if you want info on playing video and audio. You've got a lot more info in this article than the book provides. There's only one bit of info I have not covered that the book explains, and it's this. The AudioVideoPlayback functionally in the C# portion of DirectX 9 is not meant to be complete, but just enough to allow simple playback of video files. There are no plans to update/improve/add to this functionality in Managed DirectX. Enough said, in my opinion. I'm just not sure why they bothered.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Christian Graus
Software Developer (Senior)
Australia Australia
Programming computers ( self taught ) since about 1984 when I bought my first Apple ][. Was working on a GUI library to interface Win32 to Python, and writing graphics filters in my spare time, and then building n-tiered apps using asp, atl and asp.net in my job at Dytech. After 4 years there, I've started working from home, at first for Code Project and now for a vet telemedicine company. I owned part of a company that sells client education software in the vet market, but we sold that and I worked for the owners for five years before leaving to get away from the travel, and spend more time with my family. I now work for a company here in Hobart, doing all sorts of Microsoft based stuff in C++ and C#, with a lot of T-SQL in the mix.

Comments and Discussions

 
QuestionError PinmemberSharath279029-Oct-12 20:19 
AnswerRe: Error PinmvpChristian Graus29-Oct-12 20:22 
GeneralRe: Error PinmemberSharath279029-Oct-12 20:29 
GeneralRe: Error PinmvpChristian Graus29-Oct-12 20:42 
QuestionHow can i play network video stream(e. g. mms://192.168.4.198/vga02) in c#.net windows form using AudioVideoPlayback namespace of managed DirectX9? PinmemberVarsha Parab14-Dec-11 16:53 
Questionerror Pinmember~Dim~29-Nov-10 4:36 
Generalhelp. Pinmemberdkhoa4713-May-10 6:05 
GeneralMy vote of 1 Pinmemberjessca23-Feb-10 0:14 
GeneralSeeking with CurrentPosition does not update always Pinmembershultas10-Aug-09 15:05 
Questionline in audio and read data from click button Pinmemberleobluegurl11-Mar-09 8:49 
GeneralLive example of the programming skills of guys at MS PinmemberAnkit Rajpoot4-Dec-08 6:59 
QuestionOverride issue Pinmembertowerturtle23-Aug-08 9:29 
GeneralHandles PinmemberMember 40519743-Mar-08 0:27 
QuestionProblem PinmemberIbrahim Dwaikat23-Oct-07 9:21 
QuestionRe: Problem PinmemberVu-Loc20-Nov-07 8:49 
AnswerRe: Problem PinmemberIbrahim Dwaikat20-Nov-07 9:31 
QuestionHow to merge two videos PinmemberQaiserZaman26-Jan-07 7:49 
QuestionFullscreen Problem Pinmembererman.olca3-Jan-07 13:54 
Questionhandel tempo/speed of audio/video Pinmemberdatacore4-Dec-06 4:25 
AnswerRe: handle tempo/speed of audio/video PinmemberITGFanatic14-Dec-06 6:43 
GeneralMore info on audio problems [modified] PinmemberITGFanatic31-Oct-06 5:30 
GeneralMore than video? Pinmemberoamrani22-Nov-07 0:01 
GeneralHmmmmm PinmemberRichard Houltz8-Sep-06 12:45 
QuestionMemory increasing join videos PinmemberPabloDossow29-Aug-06 10:31 
AnswerRe: Memory increasing join videos Pinmemberdatacore4-Dec-06 4:21 
QuestionBetter Alternative? Pinmemberjfdoubell6-Jul-06 2:39 
QuestionExit from Fullscreen playback? Pinmemberjfdoubell28-Jun-06 22:08 
AnswerRe: Exit from Fullscreen playback? Pinmemberjfdoubell6-Jul-06 2:29 
GeneralRe: Exit from Fullscreen playback? PinmemberRonnie Andrew Magatti Lucas Silva24-May-09 5:22 
QuestionMicrosoft's answer to managed DirectShow? Pinmembernaughton6-Apr-06 9:46 
GeneralRenderToTexture PinmemberWizard_015-Mar-06 10:51 
GeneralRe: RenderToTexture PinmemberChristian Graus5-Mar-06 22:09 
GeneralInterpreting the Volume value PinmemberLes Matheson11-Feb-06 20:27 
GeneralError : LoaderLock was detected Pinmembere.lecousin9-Feb-06 4:34 
GeneralRe: Error : LoaderLock was detected PinmemberChristian Graus9-Feb-06 10:29 
GeneralRe: Error : LoaderLock was detected Pinmemberpbrush9-Jul-07 9:44 
QuestionJinxed PinmemberAnti_Everything14-Sep-05 8:32 
AnswerRe: Jinxed PinmemberAnti_Everything15-Sep-05 7:36 
GeneralNo AudioVideoPlayback NameSapce PinmemberTaoge9-Sep-05 17:04 
GeneralRe: No AudioVideoPlayback NameSapce PinmemberChristian Graus11-Sep-05 11:36 
GeneralFlash on .open Pinmemberjbanderson19-Aug-05 18:56 
GeneralRe: Flash on .open PinmemberChristian Graus21-Aug-05 13:32 
GeneralRe: Flash on .open PinmemberTunca Bergmen7-Sep-05 9:28 
GeneralCapturing Frames Pinmemberdandygroove11-Jul-05 5:09 
GeneralRe: Capturing Frames PinmemberChristian Graus11-Jul-05 13:11 
GeneralRe: Capturing Frames PinmemberDandyGroove12-Jul-05 0:20 
GeneralRe: Capturing Frames PinmemberChristian Graus12-Jul-05 13:19 
GeneralRe: Capturing Frames PinmemberDandyGroove13-Jul-05 5:10 
GeneralDirect X - Two videos at the same time Pinmemberrwaeger25-Mar-05 1:08 
GeneralRe: Direct X - Two videos at the same time PinmemberChristian Graus28-Mar-05 14:39 

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
Web01 | 2.8.140709.1 | Last Updated 13 Sep 2004
Article Copyright 2004 by Christian Graus
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid