Requirements
In order to compile this code, you must have the DirectX SDK installed on your computer.
You can download the latest version of the SDK from here., for free.
Introduction
Perhaps, at one time or another, you have wondered how to play a video file from your application. If so, you may or may not have looked into how to accomplish this task. There are a few different ways to play video files from within a C# application. In this tutorial, I will show how to create a simple video player using the DirectX SDK. In particular, I will be taking advantage of the DirectX AudioVideoPlayback library. This library contains a few classes and methods that make playing videos inside an application very simple.
Referencing the Microsoft.DirectX.AudioVideoPlayback Namespace
Before we can use the classes provided in the DirectX AudioVideoLibrary, we must add a reference to the Microsoft.DirectX.AudioVideoPlayback assembly. To do this, right-click on your project references in the solution explorer, and select Add Reference… In the dialog that appears select the assembly, as shown below:

Now, you can add the Microsoft.DirectX.AudioVideoPlayback namespace to your references:
using Microsoft.DirectX.AudioVideoPlayback;
Designing the Video Player
Now we can design the layout of our video player.
From the toolbox, drag one panel and four buttons to your form.
Arrange the controls you just added like this:

Now that we have the design of our simple video player finished, we can start adding out code.
First, at the top of your Form class, add this private member field:
Video video;
The Video class contains several methods that allow us to very easily load and play video files. This class also contains a few methods that allow us to control the videos playback in a very simple, straight forward fashion. You’ll see just how simple it is in just a second.
First, we need to add an open file dialog to our form:

Loading a Video
Now let’s add the some code to load a video file. Create an event handler for the load button by double-clicking the button.
Now, add this code to the event handler:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// store the original size of the panel
int width = viewport.Width;
int height = viewport.Height;
// load the selected video file
video = new Video(openFileDialog1.FileName);
// set the panel as the video object’s owner
video.Owner = viewport;
// stop the video
video.Stop();
// resize the video to the size original size of the panel
viewport.Size = new Size(width, height);
}
Let me take a moment to explain this code.
The very first thing we do when the user clicks on the load button is show the open file dialog that we added a little earlier. If the user selects a file, we then load it.
Next, we need to store the original width and height of the panel (which I have named viewport). Why do we need to do this? When you load a file, the panel is resized to the size of the video. We need to store the original width and height so that we can then reset the panel’s size after the video file is loaded.
Once we store the original size of the panel, we instantiate the video object, and pass the file path that the user selected as our argument. This creates a new video object, and loads the video file. We then set the video’s owner, which is just the control where the video will be played.
Next, we stop the video. This is not entirely necessary, since the video is already stopped. The reason we do this is because, otherwise, the video will not be visible until the user actually begins to play the video. So this makes the video visible, so that the user is not looking at a blank panel when they load a video.
Finally, we reset the size of the panel.
Playing a Video
Alright, now that we have given the user the ability to load a video, let’s allow them to play one.
From design view, double-click on the play button to create an event handler.
In this event handler, add this code:
if (video.State != StateFlags.Running)
{
video.Play();
}
This code is very simple. First we check to see if the video is already playing. If it is not, we play the video using the video class’ Play method. Since we check the state of the video before playing, the user cannot play the video if it’s already running (makes sense right?
Pausing a Video
This code is going to be very similar to the code to play the video:
if (video.State == StateFlags.Running)
{
video.Pause();
}
The only time we need to pause the video is while it’s running.
Stopping a Video
Again, this code is very similar to the code for playing and pausing:
if (video.State != StateFlags.Stopped)
{
video.Stop();
}
If the video is playing or paused, we stop the video.
Building and Running Your Application
That’s it. All that’s left is to build and run your application.
Select Debug>Start Debugging, or press F5.
NOTICE: You may raise this exception during runtime:

If so, you can fix this be removing LoaderLock…
First, select Exceptions from the Debug menu:

Then, in the dialog that appears, select Managed Debugging Assistants:

Scroll down to LoaderLock and de-select it from the Thrown column:

Build and Run your application.
Conclusion
Well, this ends my tutorial on playing videos in a Windows forms application. If you have any questions, I will be glad to answer them. Also, any feedback (positive or negative) is welcome.
For more information on DirectX, you can visit this MSDN page..




MultiQuote








|