使用c#.NET实现视频播放

来源:互联网 发布:地球人套料软件 编辑:程序博客网 时间:2024/06/05 15:33
使用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;; 





}  
  
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 自己的车借给别人撞死了人怎么办 无证驾驶报别人驾驶证被扣车怎么办 交了强制险但驾驶证过期了怎么办 考驾照科目一身份证掉了怎么办 驾照科目二考试身份证丢了怎么办 身份证遗失又要参加考试怎么办啊 驾驶本到期换本有扣分怎么办 b2驾驶证六年到期有扣分怎么办 驾考有效期是几年过期了怎么办? 驾考要过期了科四补考没过怎么办 驾驶证过期了可副业丢了怎么办 外地人北京驾驶本到期换本怎么办 报考驾照时电话号码填错了怎么办 报考驾照时电话填错了怎么办 邢台开三轮车驾证扣12分怎么办 新c1驾照扣满6分怎么办 b2驾照酒驾降级后再次酒驾怎么办 我b2驾照扣了9分怎么办 开别人的车出了事故怎么办 骑摩托车行驶证年检过期了怎么办 在两个城市车船税交重复了怎么办 车子被撞对方全责不赔钱怎么办 驾驶证暂扣期间该审证了怎么办 号码预约被不小心取消了怎么办 老婆出轨要跟我离婚我不想离怎么办 老婆要离婚我不想离电话拉黑怎么办 驾驶证到期换证体检有色弱怎么办 在长沙雨花区考的驾照丢了怎么办 两个户口注销了一个驾证怎么办 车管所发的初始密码弄丢了怎么办? 网上预约驾照考试密码忘记了怎么办 车是温州牌照掉了行驶证怎么办? 触犯了刑法第80条伪造印章怎么办 使用假行驶证被交警查扣车怎么办 摩托车被交警扣了没行驶证怎么办 摩托车行驶证丢了被交警抓到怎么办 摩托车没有行驶证被交警扣了怎么办 有小车执照要大在执照怎么办 报考的南京驾照不退学费怎么办 驾照报名可以退吗?不退怎么办 科目三学时已满公里数未满怎么办