C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,350 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,487 people online right now. Registration is fast and FREE... Join Now!




Adding Video to an Application

 
Reply to this topicStart new topic

> Adding Video to an Application, Adding videos to Windows forms applications using DirectX

lesPaul456
Group Icon



post 21 Jun, 2009 - 03:57 PM
Post #1


Adding Video to an Application


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:

IPB Image

Now, you can add the Microsoft.DirectX.AudioVideoPlayback namespace to your references:

csharp

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:

IPB Image

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:

csharp

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:

IPB Image

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:

csharp

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:

csharp

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? smile.gif ).

Pausing a Video

This code is going to be very similar to the code to play the video:

csharp

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:

csharp

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:

IPB Image

If so, you can fix this be removing LoaderLock…

First, select Exceptions from the Debug menu:

IPB Image

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

IPB Image

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

IPB Image

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..
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

wtkm
*



post 30 Jun, 2009 - 07:22 AM
Post #2
Hi, thanks for the tutorial.
Could you please guide me how to do the filmstrip on the video e.g. extract frames as bitmaps
Go to the top of the page
+Quote Post

lesPaul456
Group Icon



post 30 Jun, 2009 - 11:25 AM
Post #3
You'll probably need to look into using DirectShow.Net, or the Windows Media Format SDK to accomplish something like this.
Go to the top of the page
+Quote Post

Anunnaki
*



post 2 Jul, 2009 - 08:24 AM
Post #4
Hello,

Wil you pleaseguid me how to add a video to website. I mean just like Youtube.

Thx
Anunnaki
Go to the top of the page
+Quote Post

Smurphy
Group Icon



post 13 Jul, 2009 - 12:48 PM
Post #5
I have a question. Will this load any codec video or just Wmv? Say i have a flash video or something will the direct x pick that up?
Go to the top of the page
+Quote Post

lesPaul456
Group Icon



post 14 Jul, 2009 - 09:15 AM
Post #6
QUOTE(Smurphy @ 13 Jul, 2009 - 02:48 PM) *

I have a question. Will this load any codec video or just Wmv? Say i have a flash video or something will the direct x pick that up?


The only file types that I can say for sure is .wmv and .avi since I have used both types in applications before.

I'm not sure about flash video's, but I'll look into it.
Go to the top of the page
+Quote Post

GangstaPenguin
*



post 2 Aug, 2009 - 07:19 PM
Post #7
Thanks, this helped alot.
Go to the top of the page
+Quote Post

harynice
*



post 3 Sep, 2009 - 11:32 PM
Post #8
share the cod plzz....
Go to the top of the page
+Quote Post

Fuingurth
Group Icon



post 27 Sep, 2009 - 07:40 AM
Post #9
I get one exception when i run this app. Loading the video is fine, but when i press the play button, there is an error in this bit of code
CODE

video = new Video(openFileDialog1.FileName);

the only thing it says is that the exception was unhandled. I know that i could solve that by using try and catch, but I want to actually be able to play the videos. I have tried all kinds of different videos, videofiles, even just audio files. None of them are able to actually play. If you could help me out that would be great! TY
Go to the top of the page
+Quote Post

lesPaul456
Group Icon



post 2 Oct, 2009 - 09:53 AM
Post #10
QUOTE(Fuingurth @ 27 Sep, 2009 - 09:40 AM) *

I get one exception when i run this app. Loading the video is fine, but when i press the play button, there is an error in this bit of code
CODE

video = new Video(openFileDialog1.FileName);

the only thing it says is that the exception was unhandled. I know that i could solve that by using try and catch, but I want to actually be able to play the videos. I have tried all kinds of different videos, videofiles, even just audio files. None of them are able to actually play. If you could help me out that would be great! TY


In order for me to help, I need more information. What type of files are the videos? Make sure they are either .avi or .wmv.
Wrap the code in a Try/Catch block so you can get the exact error, and then post that error.
Go to the top of the page
+Quote Post

Fuingurth
Group Icon



post 27 Oct, 2009 - 03:55 PM
Post #11
well i first tried an avi , then a wmv, and then i tried ones such as mpg4, and the like.

i added a try catch block , but now i get an exception.
heres a screenshot of it
IPB Image


Note: the play.visible/enabled is for buttons that have been renamed and are ontop of one another.

This post has been edited by Fuingurth: 27 Oct, 2009 - 04:20 PM
Go to the top of the page
+Quote Post

lesPaul456
Group Icon



post Yesterday, 12:31 PM
Post #12
Sorry for the late reply...

The reason you are getting this error is because video is has not been initialized.

To be sure, I need to see the code where you initialize video. If you used my code exactly, you should not be getting any exceptions.

Could you please post all your code so I can help better? I'll try to back to you as soon as I can.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 06:21PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month