使用directx技術實現播放,需要引用系統的 c:/winnt/system32/quartz.dll
是從codeproject上看來的。前一陣看到有人需要這樣的代碼,所以貼出來共享一下。
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>
 /// 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()
 {
 // 該調用是 windows.forms 窗體設計器所必需的。
 initializecomponent();
 // todo: 在 initializecomponent 調用后添加任何初始化
 }
 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;
 }
 }
 //工作目錄
 private string mworkpath=null;
 public string workpath
 {
 set
 {
 this.mworkpath=value;
 }
 }
 /// <summary> 
 /// 清理所有正在使用的資源。
 /// </summary>
 protected override void dispose( bool disposing )
 {
 if( disposing )
 {
 if(components != null)
 {
 components.dispose();
 }
 }
 base.dispose( disposing );
 }
 #region 組件設計器生成的代碼
 /// <summary> 
 /// 設計器支持所需的方法 - 不要使用代碼編輯器 
 /// 修改此方法的內容。
 /// </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 = "準備";
 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 打開文件
 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 = "停止"; break;
 case mediastatus.paused : statusbarpanel1.text = "暫停 "; break;
 case mediastatus.running: statusbarpanel1.text = "播放"; break;
 case mediastatus.stopped: statusbarpanel1.text = "停止"; 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;
 }
 }
 }
}
,歡迎訪問網頁設計愛好者web開發。