Playing AVI Files using DirectX 9 with C# and .NET

Figure 1 - Kangaroo Hopping  Movie

Introduction

Just got back from a three week trip to Australia and all I can say is, "Give me a home among the gum trees." (Australian programmers will get what I'm saying here.). Most people know what a kangaroo looks like, but most haven't seen one up close. Needless to say, they are strange creatures if you are coming from the New York City. Unfortunately, the AVI kangaroo video taken by the digital camera was too large to upload to the site due to the size of the file. In any case, some of you are probably asking yourselves, "Kangaroos or not, how exactly do I play an AVI file in .NET?". This article will show you how by demonstrating the implementation of a WinForm Video Player. 

The Player

The Video Player takes advantage of the DirectX AudioVideoPlayback library. This library mindlessly allows you to play videos inside a Video object. The methods of the Video class are very straight forward (e.g. Play, Stop, Pause) and you simply construct the video object with the name of the file (e.g. "roo.avi").  The tables below illustrate some important  methods and properties of the Video class.

Method Description
Open(string filename) Opens an avi file with the passed parameter path name
Play Plays the video
Stop Stops the video
Pause Pauses the video
SeekCurrentPosition(double time,  SeekPositionFlags) Moves to a particular time position in the avi file relative to the SeekPositionFlag.  Time is in 1 x 10-7 seconds (or  0.1 microseconds)
StopWhenReady Waits for the video object to be ready for stopping, and stops the video

Property Type Description
Owner System.Windows.Forms.Control Sets The Window Forms Control that contains the video.
Fullscreen bool Sets whether or not the video is shown fullscreen
Size Size Sets the size of the video
Duration double Gets the time (in seconds) of the full video
CurrentPosition double Sets the position of the video
Audio Microsoft.DirectX.AudioVideoPlayback.Audio Audio object allows you to control things like the volume, balance and playing of the sound in the video

Coding

You must have DirectX 9 SDK installed to use the avi playing feature described in this article.  You can get this SDK from the Microsoft MSDN site.  (Note:  It may require an MSDN subscription to get the full SDK, but the redistributable that runs the video is a free download).  Once you have the DirectX SDK installed, you can include the Microsoft.DirectX.AudioVideoPlayback  assembly as a reference to your project.  Just right click on your project References in the Solution Explorer and add the reference as shown below.  (If this reference isn't in the .NET assembly list, you probably don't have the DirectX 9 SDK installed):

Figure 2 - Adding the DirectX AudioVideoPlayback Reference

Now you just need to add the using statement in your Form to begin using the Video class:

  1. using Microsoft.DirectX.AudioVideoPlayback;  
So now let's take a look at the implementation of the Video Class. The first step is to construct a Video object with the name of the video file which we retrieve from an OpenFile dialog.  We then assign the Video.Owner property to the panel inside our winform.  Also we resize the video to fit the original dimensions of the panel.  Finally we quickly play and pause the video, in order to see the first frame.
  1. /// <summary>  
  2. /// opens a video from an avi file  
  3. /// and plays the first frame inside the panel  
  4. /// </summary>  
  5. void OpenVideo()  
  6. {  
  7.     openFileDialog1.InitialDirectory = Application.StartupPath;  
  8.     if (openFileDialog1.ShowDialog() == DialogResult.OK)  
  9.     {  
  10.         // open the video  
  11.         // remember the original dimensions of the panel  
  12.         int height = videoPanel.Height;  
  13.         int width = videoPanel.Width;  
  14.         // dispose of the old video to clean up resources  
  15.         if (_video != null)  
  16.         {  
  17.             _video.Dispose();  
  18.         }  
  19.         // open a new video  
  20.         _video = new Video(openFileDialog1.FileName);  
  21.         // assign the win form control that will contain the video  
  22.         _video.Owner = videoPanel;  
  23.         // resize to fit in the panel  
  24.         videoPanel.Width = width;  
  25.         videoPanel.Height = height;  
  26.         // play the first frame of the video so we can identify it  
  27.         _video.Play();  
  28.         _video.Pause();  
  29.     }  
  30.     // enable video buttons  
  31.     ControlLogic();  
  32. }  
That's really all there is to using the video object.  If we want to play the video until the end, we simply call the Play method on the video object.
  1. /// <summary>  
  2. /// Plays the video  
  3. /// </summary>  
  4. /// <param name="sender"></param>  
  5. /// <param name="e"></param>  
  6. private void btnPlay_Click(object sender, System.EventArgs e)  
  7. {  
  8.     if (_video != null)  
  9.     {  
  10.         _video.Play();  
  11.     }  
  12. }  
Conclusion

The Video class in the Microsoft.DirectX.AudioVideoPlayback library is a powerful graphics class with a very simple implementation.  Using this class, you can create powerful applications that include your favorite movie clips in the common avi format.  I suspect it will play other movie formats as well (such as mpeg), however, I haven't tried it.  Good luck with this powerful multimedia tool and using it to see sharp videos.


Similar Articles