使用NET實現視頻播放

使用DirectX技術實現播放,需要引用系統的 C:/WINNT/system32/quartz.dll
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);

本篇教程來源於 完全教程網 原文鏈接:http://www.pcstu.com/program/Asp_net/sl/20070210/19238.html

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;


本篇教程來源於 完全教程網 原文鏈接:http://www.pcstu.com/program/Asp_net/sl/20070210/19238_3.html

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;
}
}


}
}


本篇教程來源於 完全教程網 原文鏈接:http://www.pcstu.com/program/Asp_net/sl/20070210/19238_4.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章