使用.NET实现视频播放

视频播放

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

}
}

时间: 2024-09-02 19:48:55

使用.NET实现视频播放的相关文章

android-微信聊天界面气泡形式的小视频播放如何实现?

问题描述 微信聊天界面气泡形式的小视频播放如何实现? 用.9图实现时会有边框,而微信里的效果应该不是这样实现的,连右边的小三角也是视频画面,有没有大神指点下这是如何实现的? 解决方案 产生一个图片,点击以后打开新的activity播放 解决方案二: 你想实现什么效果还用疑问句?谁也不知道呀

视频播放的那些事

视频作为淘宝教育业务的基础服务,本文根据自身在手淘中视频播放的实践,谈谈在手淘中视频播放遇到的问题及其解决方案. 播放器 在手淘过去一年多的历史长河中存在五种类型的播放器. 原生 HTML5 video 标签 Android 5.3.2 版本之后的 UC 内核增强 SAC 播放器 Android 5.4.9 版本之后的 UC HAC 播放器 Android 5.3.2 版本之前的 Glue native 播放器 Android 5.3.2 版本及其之后的 PlayBuddy 播放器 下面从支持平

红蜻蜓抓图精灵为何抓视频播放器画面结果一片漆黑?

  用户普遍反映红蜻蜓抓图精灵(RdfSnap)无法对视频播放器中的视频进行正确捕捉,表现为只能捕捉播放器框架,而播放画面处只有黑背底没有图像,这个问题可以通过降低视频播放器的视频加速性能来解决,以下是几种播放器的设置方法: 1)RealOne 播放器:鼠标点击 RealOne 播放器菜单中的[工具]>>[首选项],在弹出的[首选项]窗口左侧列表中选择[硬件],将右下方[视频卡兼容性]调整到中间或最左端; 2)Real Player 播放器:鼠标点击 Real Player 播放器菜单中的[视

网页视频播放器程序代码通用代码

在给客户做个程序时,突然遇到个问题,就是产品页用户提交视频播放文件时,如何根据提交的网址内的视频格式进行正确的播放呢....郁闷了一会,想好了思路,说动手就动手... 思路是先取得文件的类型,并根据类型选择不同的网页播放器代码..三下五去二.....同时为了代码在以后的复用性,写成了通用的调用函数.方便以后在别的系统中调用.. 现发布源代码如下: 以下为引用的内容:Sub SelPlay(strUrl,strWidth,StrHeight)Dim Exts,isExtIf strUrl <>

网页设计参考:WEBJX收集网页视频播放器的界面设计案例

文章描述:20个视频播放器界面欣赏及点评. 网页设计中非常细节的东西,比如就一个描边,有时就造成了用户心理感受上的差别,进而影响用户的使用偏好,直至对整个设计的成功与否以及作品的商业价值产生非常大的影响.类似于"千里之堤毁于蚁穴"和"蝴蝶效应"的感觉.夸大其词吗?绝对不!细节的力量我认为在网页设计中有着无穷的魅力.认真想想那些你个人认为优秀的设计作品,其中是什么东西打动了你?真的很多时候就是一种描述不出来的颜色,一些1像素的高光,或者是一种质感. 例如下面的这20个

我优化YouTube视频播放页面的故事

  三年前,当我还在YouTube做一名web程序员时,有一位资深的工程师发了一通牢骚,说播放视频的页面体积太大.这个页面体积已经膨胀到了高达 1.2MB,包含有数十次的请求.这个工程师公开的宣称说,"如果他们Quake能在100KB的体积下克隆出我们的页面,我们没有理由达不到这个体 积!"因为我同意他的观点,并且我正在找新的任务,于是就决定接受这个挑战,让YouTube的视频播放页面的体积减到100KB以下.那天晚上从旧金山 回家的火车上,我编写了一个基本的原型.我决定限制页面上的功

网页中插入视频播放代码全集

插入|视频播放|网页 1.avi格式 代码片断如下: <object id="video" width="400" height="200" border="0" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"><param name="ShowDisplay" value="0">&l

Visual C#使用DirectX实现视频播放

visual|视频播放 很多人第一次接触到DirectX大都是通过游戏,至于安装.升级DirectX的原因无非是满足游戏运行的需要.Direct X其实是微软公司推出的一个为Windows平台的多媒体API函数库,它提供标准接口来与显卡和声卡.输入设备等进行交互.如果没有这组标准API函数库,那你就需要为每一种显卡.声卡的每个组合和每种类型的键盘.鼠标和游戏杆编写不同的代码.这不又回到了以前的Dos时代.为了解决这个问题,微软公司推出了DirectX.DirectX从具体的硬件中抽象出来,并且将

技巧:自制Flash FLV视频播放器

技巧|视频播放 大家好,前不久笔者将CenFun Music Player加入flv视频播放功能,这里单独拿出来给大家做一个Flash FLV播放器(仅ActionScript控制,不使用 FLVPlayback 组件),如有不当之处还望指正.下面就打开您的Macromedia Flash Player 8(推荐)开始吧! 首先新建文档,设置背景色黑色,其他默认,然后建四个图层. 第一层用来放视频元件,方法如下: 1,在"库"面板("窗口">"库&q

网页视频播放器代码大全

收集最常用的网页中嵌入视频代码大全,各种各样的网页视频播放器代码. 网页中嵌入视频代码综合完全版 1.avi格式 代码片断如下: 程序代码 <objectid="video"width="400"height="200"border="0"classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"> <paramname="ShowD