Click here to Skip to main content
15,879,095 members
Articles / Multimedia / DirectX
Article

Problems in the AudioVideoPlayback namespace of managed DirectX9

Rate me:
Please Sign up or sign in to vote.
4.68/5 (38 votes)
13 Sep 2004CPOL6 min read 254.4K   2K   39   82
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:

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

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

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

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

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


Written By
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 Pin
Sharath279029-Oct-12 20:19
Sharath279029-Oct-12 20:19 
AnswerRe: Error Pin
Christian Graus29-Oct-12 20:22
protectorChristian Graus29-Oct-12 20:22 
GeneralRe: Error Pin
Sharath279029-Oct-12 20:29
Sharath279029-Oct-12 20:29 
GeneralRe: Error Pin
Christian Graus29-Oct-12 20:42
protectorChristian Graus29-Oct-12 20:42 
Look at the inner exception and try to fix the problem. Or look for another way to play videos.
Christian Graus

Driven to the arms of OSX by Vista.

Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

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? Pin
Varsha Parab14-Dec-11 16:53
Varsha Parab14-Dec-11 16:53 
Questionerror Pin
~Dim~29-Nov-10 4:36
~Dim~29-Nov-10 4:36 
Generalhelp. Pin
dkhoa4713-May-10 6:05
dkhoa4713-May-10 6:05 
GeneralMy vote of 1 Pin
jessca23-Feb-10 0:14
jessca23-Feb-10 0:14 
GeneralSeeking with CurrentPosition does not update always Pin
shultas10-Aug-09 15:05
shultas10-Aug-09 15:05 
Questionline in audio and read data from click button Pin
leobluegurl11-Mar-09 8:49
leobluegurl11-Mar-09 8:49 
GeneralLive example of the programming skills of guys at MS Pin
Ankit Rajpoot4-Dec-08 6:59
Ankit Rajpoot4-Dec-08 6:59 
QuestionOverride issue Pin
TowerTurtle23-Aug-08 9:29
TowerTurtle23-Aug-08 9:29 
GeneralHandles Pin
James Stevenson3-Mar-08 0:27
James Stevenson3-Mar-08 0:27 
QuestionProblem Pin
Ibrahim Dwaikat23-Oct-07 9:21
Ibrahim Dwaikat23-Oct-07 9:21 
QuestionRe: Problem Pin
Vu-Loc20-Nov-07 8:49
Vu-Loc20-Nov-07 8:49 
AnswerRe: Problem Pin
Ibrahim Dwaikat20-Nov-07 9:31
Ibrahim Dwaikat20-Nov-07 9:31 
QuestionHow to merge two videos Pin
QaiserZaman26-Jan-07 7:49
QaiserZaman26-Jan-07 7:49 
QuestionFullscreen Problem Pin
erman.olca3-Jan-07 13:54
erman.olca3-Jan-07 13:54 
Questionhandel tempo/speed of audio/video Pin
datacore4-Dec-06 4:25
datacore4-Dec-06 4:25 
AnswerRe: handle tempo/speed of audio/video Pin
ITGFanatic14-Dec-06 6:43
ITGFanatic14-Dec-06 6:43 
GeneralMore info on audio problems [modified] Pin
ITGFanatic31-Oct-06 5:30
ITGFanatic31-Oct-06 5:30 
GeneralMore than video? Pin
oamrani22-Nov-07 0:01
oamrani22-Nov-07 0:01 
GeneralHmmmmm Pin
Richard Houltz8-Sep-06 12:45
Richard Houltz8-Sep-06 12:45 
QuestionMemory increasing join videos Pin
PabloDossow29-Aug-06 10:31
PabloDossow29-Aug-06 10:31 
AnswerRe: Memory increasing join videos Pin
datacore4-Dec-06 4:21
datacore4-Dec-06 4:21 

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.