Use .NET to implement video playback

xiaoxiao2021-03-06  40

Play with DirectX technology, you need to reference the system's c: /winnt/system32/quartz.dll is from CodeProject. I saw someone who needs such a code, so I possess it. using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using QuartzTypeLib; namespace Greystar.FrameWork.ToolKits {///

/// FrmTV Abstract illustration. /// 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. InitializationComponent ();

// Todo: Add any initialization after INITIALIZECMPONENT 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;}} ///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION component designer generated code ///

/// designer supports the required method - Do not use the code editor // to modify the contents of this method.

/// 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.Componen tModel.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.ImageListStreame) (Resources.get) Object ("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.dockstyle.Fill; this.pal1.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 = "ready" ; 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; // // fmtv // 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 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 = mediatatus.paused; Break;}

Case 3: {m_objmediacontrol.stop (); m_objmediaposition.currentposition = 0; m_currentstatus = mediatatus.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) {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 = "Pause"; 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;}}

}

转载请注明原文地址:https://www.9cbs.com/read-67979.html

New Post(0)