Use. NET to achieve video playback

Source: Internet
Author: User
Video playback using DirectX technology to achieve playback, need to refer to the system C:\WINNT\system32\quartz.dll
It's from the CodeProject. A while ago see someone needs such a code, so paste it out to share.

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;
Using QuartzTypeLib;

Namespace Greystar.FrameWork.ToolKits
{
<summary>
Summary description of the FRMTV.
</summary>
public class FrmTV:System.Windows.Forms.UserControl
{
Private System.Windows.Forms.ToolBar toolBar1;
Private System.Windows.Forms.ToolBarButton ToolBarButton1;
Private System.Windows.Forms.ToolBarButton ToolBarButton2;
Private System.Windows.Forms.ToolBarButton ToolBarButton3;
Private System.Windows.Forms.Panel Panel1;
Private System.Windows.Forms.ImageList ImageList1;
Private System.Windows.Forms.Timer timer1;
Private System.Windows.Forms.ToolBarButton ToolBarButton4;
Private System.ComponentModel.IContainer components;

Private Const int WM_APP = 0x8000;
Private Const int wm_graphnotify = Wm_app + 1;
Private Const int ec_complete = 0x01;
Private Const int ws_child = 0x40000000;
Private Const int ws_clipchildren = 0x2000000;

Private Filgraphmanager m_objfiltergraph = null;
Private Ibasicaudio M_objbasicaudio = null;
Private Ivideowindow M_objvideowindow = null;
Private imediaevent m_objmediaevent = null;
Private Imediaeventex M_objmediaeventex = null;
Private imediaposition m_objmediaposition = null;
Private IMediaControl M_objmediacontrol = null;
Private System.Windows.Forms.StatusBar statusBar1;
Private System.Windows.Forms.StatusBarPanel StatusBarPanel1;
Private System.Windows.Forms.StatusBarPanel StatusBarPanel2;
Private System.Windows.Forms.StatusBarPanel statusBarPanel3;

Enum Mediastatus {None, Stopped, paused, Running};

Private Mediastatus m_currentstatus = Mediastatus.none;

Public FRMTV ()
{
This call is required for the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the InitializeComponent call

}
Public FRMTV (Envdte._dte dte,string Workpath)
{
InitializeComponent ();
This.mdte=dte;
This.mworkpath=workpath;
}
Private _dte Mdte=null;
Public _dte DTE
{
Set
{
Mdte=value;
}

Get
{
return mdte;
}
}

Working directory
private string Mworkpath=null;
public string Workpath
{
Set
{
This.mworkpath=value;
}
}
<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region Component Designer
<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{
this.components = new System.ComponentModel.Container ();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof (FRMTV));
THIS.TOOLBAR1 = new System.Windows.Forms.ToolBar ();
This.toolbarbutton4 = new System.Windows.Forms.ToolBarButton ();
This.toolbarbutton1 = new System.Windows.Forms.ToolBarButton ();
This.toolbarbutton2 = new System.Windows.Forms.ToolBarButton ();
This.toolbarbutton3 = new System.Windows.Forms.ToolBarButton ();
This.imagelist1 = new System.Windows.Forms.ImageList (this.components);
This.panel1 = new System.Windows.Forms.Panel ();
This.timer1 = new System.Windows.Forms.Timer (this.components);
THIS.STATUSBAR1 = new System.Windows.Forms.StatusBar ();
This.statusbarpanel1 = new System.Windows.Forms.StatusBarPanel ();
This.statusbarpanel2 = new System.Windows.Forms.StatusBarPanel ();
This.statusbarpanel3 = new System.Windows.Forms.StatusBarPanel ();
((System.ComponentModel.ISupportInitialize) (This.statusbarpanel1)). BeginInit ();
((System.ComponentModel.ISupportInitialize) (This.statusbarpanel2)). BeginInit ();
((System.ComponentModel.ISupportInitialize) (THIS.STATUSBARPANEL3)). BeginInit ();
This. SuspendLayout ();
//
ToolBar1
//
This.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
This.toolBar1.Buttons.AddRange (new system.windows.forms.toolbarbutton[] {
This.toolbarbutton4,
This.toolbarbutton1,
This.toolbarbutton2,
This.toolbarbutton3});
This.toolBar1.DropDownArrows = true;
This.toolBar1.ImageList = This.imagelist1;
This.toolBar1.Location = new System.Drawing.Point (0, 0);
This.toolBar1.Name = "ToolBar1";
This.toolBar1.ShowToolTips = true;
This.toolBar1.Size = new System.Drawing.Size (288, 28);
This.toolBar1.TabIndex = 0;
This.toolBar1.ButtonClick + = new System.Windows.Forms.ToolBarButtonClickEventHandler (This.toolbar1_buttonclick);
//
ToolBarButton4
//
This.toolBarButton4.ImageIndex = 3;
//
ToolBarButton1
//
this.toolBarButton1.Enabled = false;
This.toolBarButton1.ImageIndex = 0;
//
ToolBarButton2
//
this.toolBarButton2.Enabled = false;
This.toolBarButton2.ImageIndex = 1;
//
ToolBarButton3
//
this.toolBarButton3.Enabled = false;
This.toolBarButton3.ImageIndex = 2;
//
ImageList1
//
This.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
This.imageList1.ImageSize = new System.Drawing.Size (16, 16);
This.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer) (resources. GetObject ("Imagelist1.imagestream"));
This.imageList1.TransparentColor = System.Drawing.Color.Red;
//
Panel1
//
This.panel1.BackColor = System.Drawing.Color.Black;
This.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
This.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
This.panel1.Location = new System.Drawing.Point (0, 28);
This.panel1.Name = "Panel1";
This.panel1.Size = new System.Drawing.Size (288, 252);
This.panel1.TabIndex = 4;
//
Timer1
//
This.timer1.Enabled = true;
This.timer1.Tick + = new System.EventHandler (This.timer1_tick);
//
StatusBar1
//
This.statusBar1.Location = new System.Drawing.Point (0, 260);
This.statusBar1.Name = "StatusBar1";
This.statusBar1.Panels.AddRange (new system.windows.forms.statusbarpanel[] {
This.statusbarpanel1,
This.statusbarpanel2,
THIS.STATUSBARPANEL3});
This.statusBar1.ShowPanels = true;
This.statusBar1.Size = new System.Drawing.Size (288, 20);
This.statusBar1.TabIndex = 5;
//
StatusBarPanel1
//
This.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
This.statusBarPanel1.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None;
This.statusBarPanel1.Text = "Preparation";
This.statusBarPanel1.Width = 144;
//
StatusBarPanel2
//
This.statusBarPanel2.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
This.statusBarPanel2.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
This.statusBarPanel2.Text = "00:00:00";
This.statusBarPanel2.Width = 64;
//
StatusBarPanel3
//
This.statusBarPanel3.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
This.statusBarPanel3.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
This.statusBarPanel3.Text = "00:00:00";
This.statusBarPanel3.Width = 64;
//
Frmtv
//
This. BackColor = System.Drawing.SystemColors.Control;
This. Controls.Add (THIS.STATUSBAR1);
This. Controls.Add (THIS.PANEL1);
This. Controls.Add (THIS.TOOLBAR1);
This. Name = "FRMTV";
This. Size = new System.Drawing.Size (288, 280);
This. SizeChanged + = new System.EventHandler (this. frmtv_sizechanged);
((System.ComponentModel.ISupportInitialize) (This.statusbarpanel1)). EndInit ();
((System.ComponentModel.ISupportInitialize) (This.statusbarpanel2)). EndInit ();
((System.ComponentModel.ISupportInitialize) (THIS.STATUSBARPANEL3)). EndInit ();
This. ResumeLayout (FALSE);

}
#endregion

private void CleanUp ()
{
if (M_objmediacontrol!= null)
M_objmediacontrol.stop ();

M_currentstatus = mediastatus.stopped;

if (M_objmediaeventex!= null)
M_objmediaeventex.setnotifywindow (0, 0, 0);

if (M_objvideowindow!= null)
{
m_objvideowindow.visible = 0;
M_objvideowindow.owner = 0;
}

if (M_objmediacontrol!= null) M_objmediacontrol = NULL;
if (m_objmediaposition!= null) m_objmediaposition = NULL;
if (M_objmediaeventex!= null) M_objmediaeventex = NULL;
if (m_objmediaevent!= null) m_objmediaevent = NULL;
if (M_objvideowindow!= null) M_objvideowindow = NULL;
if (M_objbasicaudio!= null) M_objbasicaudio = NULL;
if (m_objfiltergraph!= null) m_objfiltergraph = NULL;
}

private void Toolbar1_buttonclick (object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
Switch (toolBar1.Buttons.IndexOf (E.button))
{
Case 0:
{
#region Open a file
OpenFileDialog OpenFileDialog = new OpenFileDialog ();
Openfiledialog.filter = "Media files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3| All Files|*.* ";

if (DialogResult.OK = = Openfiledialog.showdialog ())
{
CleanUp ();

M_objfiltergraph = new Filgraphmanager ();
M_objfiltergraph.renderfile (Openfiledialog.filename);

M_objbasicaudio = M_objfiltergraph as Ibasicaudio;

Try
{
M_objvideowindow = M_objfiltergraph as Ivideowindow;
M_objvideowindow.owner = (int) Panel1. Handle;
M_objvideowindow.windowstyle = Ws_child | Ws_clipchildren;
M_objvideowindow.setwindowposition (Panel1. Clientrectangle.left,
Panel1. Clientrectangle.top,
Panel1. Clientrectangle.width,
Panel1. Clientrectangle.height);
}
catch (Exception)
{
M_objvideowindow = null;
}

M_objmediaevent = M_objfiltergraph as imediaevent;

M_objmediaeventex = M_objfiltergraph as Imediaeventex;
M_objmediaeventex.setnotifywindow (int) this. handle,wm_graphnotify, 0);

M_objmediaposition = M_objfiltergraph as imediaposition;

M_objmediacontrol = M_objfiltergraph as IMediaControl;

This. Text = "DirectShow-[" + Openfiledialog.filename + "]";

M_objmediacontrol.run ();
M_currentstatus = mediastatus.running;

}
Break
#endregion

}
Case 1:
{
M_objmediacontrol.run ();
M_currentstatus = mediastatus.running;
Break
}

Case 2:
{
M_objmediacontrol.pause ();
M_currentstatus = mediastatus.paused;
Break
}

Case 3:
{
M_objmediacontrol.stop ();
m_objmediaposition.currentposition = 0;
M_currentstatus = mediastatus.stopped;
Break
}
}

Updatestatusbar ();
Updatetoolbar ();
}

private void Frmtv_sizechanged (object sender, System.EventArgs e)
{
if (M_objvideowindow!= null)
{
M_objvideowindow.setwindowposition (Panel1. Clientrectangle.left,
Panel1. Clientrectangle.top,
Panel1. Clientrectangle.width,
Panel1. Clientrectangle.height);
}
}

private void Timer1_Tick (object sender, System.EventArgs e)
{

if (M_currentstatus = = mediastatus.running)
{
Updatestatusbar ();
}
}
protected override void WndProc (ref message M)
{
if (m.msg = = wm_graphnotify)
{
int Leventcode;
int lParam1, LPARAM2;

while (true)
{
Try
{
M_objmediaeventex.getevent (out Leventcode,
Out LParam1,
Out LParam2,
0);

M_objmediaeventex.freeeventparams (Leventcode, lParam1, lParam2);

if (Leventcode = = Ec_complete)
{
M_objmediacontrol.stop ();
m_objmediaposition.currentposition = 0;
M_currentstatus = mediastatus.stopped;
Updatestatusbar ();
Updatetoolbar ();
}
}
catch (Exception)
{
Break
}
}
}

Base. WndProc (ref m);
}

private void Updatestatusbar ()
{
Switch (m_currentstatus)
{
Case MediaStatus.None:statusBarPanel1.Text = "Stop"; Break
Case MediaStatus.Paused:statusBarPanel1.Text = "suspend"; Break
Case MediaStatus.Running:statusBarPanel1.Text = "Play"; Break
Case MediaStatus.Stopped:statusBarPanel1.Text = "Stop"; Break
}

if (m_objmediaposition!= null)
{
int s = (int) m_objmediaposition.duration;
int h = s/3600;
int m = (S-(H * 3600))/60;
s = S-(h * 3600 + M * 60);

Statusbarpanel2.text = String.Format ("{0:d2}:{1:d2}:{2:d2}", H, M, s);

s = (int) m_objmediaposition.currentposition;
h = s/3600;
m = (S-(H * 3600))/60;
s = S-(h * 3600 + M * 60);

Statusbarpanel3.text = String.Format ("{0:d2}:{1:d2}:{2:d2}", H, M, s);
}
Else
{
Statusbarpanel2.text = "00:00:00";
Statusbarpanel3.text = "00:00:00";
}
}

private void Updatetoolbar ()
{
Switch (m_currentstatus)
{
Case MediaStatus.None:toolBarButton1.Enabled = false;
toolbarbutton2.enabled = false;
toolbarbutton3.enabled = false;
Break

Case MediaStatus.Paused:toolBarButton1.Enabled = true;
toolbarbutton2.enabled = false;
Toolbarbutton3.enabled = true;
Break

Case MediaStatus.Running:toolBarButton1.Enabled = false;
Toolbarbutton2.enabled = true;
Toolbarbutton3.enabled = true;
Break

Case MediaStatus.Stopped:toolBarButton1.Enabled = true;
toolbarbutton2.enabled = false;
toolbarbutton3.enabled = false;
Break
}
}


}
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.