Resources » Code Snippets » Winforms Controls

How to Play Videos in .NET WinForm Application using Visual C# and DirectX?


Posted Date: 08-May-2011  Last Updated:   Category: Winforms Controls    
Author: Member Level: Gold    Points: 20


In this article, I will explain how to Play Videos using Visual C# with the help of DirectX Library in .NET 2.0 framework. That means how to create our own WinForm Video Player Application to play videos. This Video Player can play videos in most common video format like .avi, .dat etc.



This Video Player Application takes advantage of the "Microsoft.DirectX.AudioVideoPlayback" library. This library allows to play videos inside a Video object. Some useful methods of the Video class are Play, Stop, Pause etc. And you simply construct the video object with the name including the path of the video file (e.g. "D:\Film\blue.avi"). You can maintain the resolution of the video object and can maintain the volume also. You must have DirectX 9 or higher SDK installed in your computer to make this Video Player work. You can get the latest DirectX SDK from the Microsoft Download Centre(http://download.microsoft.com). 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 of "Microsoft.DirectX.AudioVideoPlayback" by browsing and selecting "Microsoft.DirectX.AudioVideoPlayback.dll" file which should be in the "C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0" folder in your computer.

Now import "Microsoft.DirectX.AudioVideoPlayback" library by adding "using Microsoft.DirectX.AudioVideoPlayback;" in your Form.cs.

Take these controls given below in your Form:
1. Panel VideoPanel;(resize it for video resolution)
2. Button OpenFileBtn;
3. Button PlayPauseBtn;
4. Button StopBtn;
5. OpenFileDialog openFileDialog1;
6. TrackBar SeekTrackBar;
7. TextBox TimeTextBox;
8. TrackBar VolumeTrackBar;
9. Timer timer1;

Place all those control in a proper manner in your form(You can take a look on the attached image). And here is the code(Form.cs) below to make your own Video Player:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.AudioVideoPlayback;

namespace VideoPlayer
{
public partial class Form1 : Form
{
public Video MyVideo;
public int PanelWidth, PanelHeight;
public string PlayingPosition, Duration;

public Form1()
{
InitializeComponent();
timer1.Stop();
VolumeTrackBar.Minimum = 0;
VolumeTrackBar.Maximum = 10;
VolumeTrackBar.Value = 5;
}

public string CalculateTime(double Time)
{
string mm, ss, CalculatedTime;
int h, m, s, T;

Time = Math.Round(Time);
T = Convert.ToInt32(Time);

h = (T / 3600);
T = T % 3600;
m = (T / 60);
s = T % 60;

if (m < 10)
mm = string.Format("0{0}", m);
else
mm = m.ToString();
if (s < 10)
ss = string.Format("0{0}", s);
else
ss = s.ToString();

CalculatedTime = string.Format("{0}:{1}:{2}", h, mm, ss);

return CalculatedTime;
}

public int CalculateVolume()
{
//Maximum Volume/Loudest value = 0, Minimum Volume/Mute value = -10000
switch (VolumeTrackBar.Value)
{
case 1:
return -1500;
case 2:
return -1000;
case 3:
return -700;
case 4:
return -600;
case 5:
return -500;
case 6:
return -400;
case 7:
return -300;
case 8:
return -200;
case 9:
return -100;
case 10:
return 0;
default:
return -10000;
}
}

public void StartUp()
{
if (MyVideo != null)
{
MyVideo.Dispose();
}

Text = openFileDialog1.SafeFileName + " - MPlayer 2.0";
PanelWidth = VideoPanel.Width;
PanelHeight = VideoPanel.Height;

MyVideo = new Video(openFileDialog1.FileName);
MyVideo.Owner = VideoPanel;
VideoPanel.Width = PanelWidth;
VideoPanel.Height = PanelHeight;

SeekTrackBar.Minimum = Convert.ToInt32(MyVideo.CurrentPosition);
SeekTrackBar.Maximum = Convert.ToInt32(MyVideo.Duration);
SeekTrackBar.Value = 0;
PlayPauseBtn.Text = "Play";

Duration = CalculateTime(MyVideo.Duration);
PlayingPosition = "0:00:00";
TimeTextBox.Text = PlayingPosition + "/" + Duration;


MyVideo.Audio.Volume = CalculateVolume();

//To show the first frame of the video to recognize it
MyVideo.Play();
MyVideo.Pause();

}

private void OpenFileBtn_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Select video file..";
openFileDialog1.InitialDirectory = Application.StartupPath;
openFileDialog1.DefaultExt = ".avi";
openFileDialog1.Filter = "AVI files|*.avi|DAT files|*.dat|All files|*.*";
openFileDialog1.ShowDialog();
}

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
StartUp();
}

private void PlayPauseBtn_Click(object sender, EventArgs e)
{
if (MyVideo != null)
{
if (MyVideo.Playing)
{
MyVideo.Pause();
timer1.Stop();
PlayPauseBtn.Text = "Play";
}
else
{
MyVideo.Play();
timer1.Start();
PlayPauseBtn.Text = "Pause";
}
}
}

private void StopBtn_Click(object sender, EventArgs e)
{
if (MyVideo != null)
{
MyVideo.StopWhenReady();
timer1.Stop();
StartUp();
}
}

private void timer1_Tick(object sender, EventArgs e)
{
SeekTrackBar.Value = Convert.ToInt32(MyVideo.CurrentPosition);
PlayingPosition = CalculateTime(MyVideo.CurrentPosition);
TimeTextBox.Text = PlayingPosition + "/" + Duration;

if (PlayingPosition == Duration)
{
timer1.Stop();
StartUp();
}
}

private void SeekTrackBar_Scroll(object sender, EventArgs e)
{
if (MyVideo != null)
{
MyVideo.CurrentPosition = SeekTrackBar.Value;
}
else
{
SeekTrackBar.Value = 0;
}
}

private void VolumeTrackBar_Scroll(object sender, EventArgs e)
{
if (MyVideo != null)
{
MyVideo.Audio.Volume = CalculateVolume();
}
}
}
}


Reference: monojindia.brinkster.net

Attachments


Form Design


Did you like this resource? Share it with your friends and show your love!

Responses to "How to Play Videos in .NET WinForm Application using Visual C# and DirectX?"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India