• (3 Pages)
  • +
  • 1
  • 2
  • 3

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

#1 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Posted 21 June 2009 - 04:57 PM

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:

Posted Image

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:

Posted 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:

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:

Posted 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:

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:

Posted Image

If so, you can fix this be removing LoaderLockÖ

First, select Exceptions from the Debug menu:

Posted Image

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

Posted Image

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

Posted 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..

Is This A Good Question/Topic? 2
  • +

Replies To: Adding Video to an Application

#2 wtkm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 08-April 09

Posted 30 June 2009 - 08:22 AM

Hi, thanks for the tutorial.
Could you please guide me how to do the filmstrip on the video e.g. extract frames as bitmaps
Was This Post Helpful? 0
  • +
  • -

#3 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Posted 30 June 2009 - 12:25 PM

You'll probably need to look into using DirectShow.Net, or the Windows Media Format SDK to accomplish something like this.
Was This Post Helpful? 0
  • +
  • -

#4 Anunnaki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-July 09

Posted 02 July 2009 - 09:24 AM

Hello,

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

Thx
Anunnaki
Was This Post Helpful? 0
  • +
  • -

#5 Smurphy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Posted 13 July 2009 - 01: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?
Was This Post Helpful? 0
  • +
  • -

#6 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Posted 14 July 2009 - 10:15 AM

View PostSmurphy, on 13 Jul, 2009 - 02:48 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#7 GangstaPenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-August 09

Posted 02 August 2009 - 08:19 PM

Thanks, this helped alot.
Was This Post Helpful? 0
  • +
  • -

#8 harynice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-June 09

Posted 04 September 2009 - 12:32 AM

share the cod plzz....
Was This Post Helpful? 0
  • +
  • -

#9 Fuingurth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Posted 27 September 2009 - 08: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
 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
Was This Post Helpful? 0
  • +
  • -

#10 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Posted 02 October 2009 - 10:53 AM

View PostFuingurth, on 27 Sep, 2009 - 09:40 AM, said:

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
 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.
Was This Post Helpful? 0
  • +
  • -

#11 Fuingurth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Posted 27 October 2009 - 04:55 PM

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
Posted 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 October 2009 - 05:20 PM

Was This Post Helpful? 0
  • +
  • -

#12 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Posted 06 November 2009 - 01:31 PM

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.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Povilas*


Reputation:

Posted 15 April 2010 - 02:27 PM

Hi.
I'm trying to make my application be able to play video in full-screen mode. It's simple - Microsoft.DirectX.AudioVideoPlayback.Video class has Fullscreen property. Do you have any ideas how to make it possible to return back to normal (not Fullscreen) mode (for example when i press 'Esc' key or double-click on full-screen mode)?
Was This Post Helpful? 0

#14 Guest_raza*


Reputation:

Posted 10 February 2011 - 06:36 PM

Bro you have illustrated a marvellous piece of code regarding video play in C# code. My question is "I require to add a video in C# application when the form runs at very first time. I mean it is like displaying advertisement in the form. So I do not want to have a START, STOP, PAUSE or LOAD button in my application. Once I start the appplication the video should run." Most probably my video is just seconds 15 stuff. It should play iteratively. Please assisst me to get this done.
My mail address is "<Removed>"

This post has been edited by macosxnerd101: 10 February 2011 - 06:38 PM
Reason for edit:: Email removed per site policies. Please keep all work via the forums.

Was This Post Helpful? 0

#15 ZenQ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 22-February 11

Posted 22 February 2011 - 06:39 AM

I got this error when pressing the browse button

The reference for the DirectX.AudioVideoPlayback was not found in my list of .NET, so i used the one located in Windows/Microsoft.NET/DirectX for Managed Code/1.0.2902.0

note:I do have the latest Windows Media Format SDK installed. Or must I also have the DirectShow.Net?

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3