C#编写媒体播放器--Microsoft的Directx提供的DirectShow组件,该组件的程序集QuartzTypeLib.dll.
使用C#编写媒体播放器时,需要用到Microsoft的Directx提供的DirectShow组件.用该组件前需要先注册程序集QuartzTypeLib.dll.
1、用QuartzTypeLib.dll播放视频(http://i.cnblogs.com/Files.aspx下载类库)
1.1Form1.cs
using
System;
using
System.Windows.Forms;
using
QuartzTypeLib;
namespace
DirectShow
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
//设置常量
const
int
WM_APP = 0x8000;
const
int
WM_GRAPHNOTIFY = WM_APP + 1;
const
int
EC_COMPLETE = 0x01;
const
int
WS_CHILD = 0x40000000;
const
int
WS_CLIPCHILDREN = 0x2000000;
FilgraphManager m_objFilterGraph =
null
;
IBasicAudio m_objBasicAudio =
null
;
IVideoWindow m_objVidwoWindow =
null
;
IMediaEvent m_objMediaEvent =
null
;
IMediaEventEx m_objMediaEventEx =
null
;
IMediaPosition m_objMediaPosition =
null
;
IMediaControl m_objMediaControl =
null
;
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;
}
}
catch
(Exception)
{
break
;
}
}
}
base
.WndProc(
ref
m);
}
private
void
Clear()
{
if
(m_objMediaControl !=
null
)
m_objMediaControl.Stop();
if
(m_objMediaEventEx !=
null
)
m_objMediaEventEx.SetNotifyWindow(0, 0, 0);
if
(m_objVidwoWindow !=
null
)
{
m_objVidwoWindow.Visible = 0;
m_objVidwoWindow.Owner = 0;
}
if
(m_objBasicAudio !=
null
) m_objBasicAudio =
null
;
if
(m_objFilterGraph !=
null
) m_objFilterGraph =
null
;
if
(m_objMediaControl !=
null
) m_objMediaControl =
null
;
if
(m_objMediaEvent !=
null
) m_objMediaEvent =
null
;
if
(m_objMediaPosition !=
null
) m_objMediaPosition =
null
;
if
(m_objVidwoWindow !=
null
) m_objVidwoWindow =
null
;
if
(m_objMediaEventEx !=
null
) m_objMediaEventEx =
null
;
this
.Text =
"多媒体播放器"
;
timer1.Enabled =
false
;
trackBar1.Value = 0;
}
private
void
button1_Click(
object
sender, EventArgs e)
{
OpenFileDialog openFile =
new
OpenFileDialog();
openFile.Filter =
"选择播放文件|*.mpg;*.avi;*.mov;*.wma;*.wav;*.mp3|All Files|*.*"
;
if
(DialogResult.OK == openFile.ShowDialog())
{
this
.Clear();
//清理正在播放的文件
m_objFilterGraph =
new
FilgraphManager();
try
{
m_objFilterGraph.RenderFile(openFile.FileName);
m_objBasicAudio = m_objFilterGraph
as
IBasicAudio;
//图象控制接口
m_objVidwoWindow = m_objFilterGraph
as
IVideoWindow;
//声音控制接口
m_objVidwoWindow.Owner = (
int
)
this
.Handle;
//设置窗口类型
m_objVidwoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
//设置播放容器
m_objVidwoWindow.SetWindowPosition(
this
.ClientRectangle.Left,
this
.ClientRectangle.Top,
this
.ClientRectangle.Width,
this
.ClientRectangle.Height - 60);
}
catch
{
//MessageBox.Show(ex.Message);
//当出错时,为音频文件,将视频对象清空
m_objVidwoWindow =
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 = openFile.FileName +
"----"
+
this
.Text;
m_objMediaControl.Run();
//播放文件
try
{
trackBar1.LargeChange = 1;
trackBar1.Maximum = (
int
)m_objMediaPosition.Duration;
timer1.Enabled =
true
;
}
catch
{ }
}
}
private
void
button2_Click(
object
sender, EventArgs e)
{
//播放
if
(m_objMediaControl !=
null
)
m_objMediaControl.Run();
}
private
void
button3_Click(
object
sender, EventArgs e)
{
//暂停
m_objMediaControl.Pause();
}
private
void
button4_Click(
object
sender, EventArgs e)
{
//停止
m_objMediaControl.Stop();
m_objMediaPosition.CurrentPosition = 0;
this
.Clear();
}
private
void
button5_Click(
object
sender, EventArgs e)
{
//快退
if
(m_objMediaPosition.CurrentPosition >= 1)
m_objMediaPosition.CurrentPosition = m_objMediaPosition.CurrentPosition - 1;
else
m_objMediaPosition.CurrentPosition = 0;
}
private
void
button6_Click(
object
sender, EventArgs e)
{
//快进
m_objMediaPosition.CurrentPosition = m_objMediaPosition.CurrentPosition + 1;
}
private
void
timer1_Tick(
object
sender, EventArgs e)
{
//显示文件播放的进度
trackBar1.Value = (
int
)m_objMediaPosition.CurrentPosition;
}
private
void
trackBar1_Scroll(
object
sender, EventArgs e)
{
//根据滚动进度条,来播放文件
m_objMediaPosition.CurrentPosition = trackBar1.Value;
}
}
}
1.2 Form1.Designer.cs
namespace
DirectShow
{
partial
class
Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private
System.ComponentModel.IContainer components =
null
;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected
override
void
Dispose(
bool
disposing)
{
if
(disposing && (components !=
null
))
{
components.Dispose();
}
base
.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private
void
InitializeComponent()
{
this
.components =
new
System.ComponentModel.Container();
this
.button1 =
new
System.Windows.Forms.Button();
this
.button2 =
new
System.Windows.Forms.Button();
this
.button3 =
new
System.Windows.Forms.Button();
this
.button4 =
new
System.Windows.Forms.Button();
this
.button5 =
new
System.Windows.Forms.Button();
this
.button6 =
new
System.Windows.Forms.Button();
this
.trackBar1 =
new
System.Windows.Forms.TrackBar();
this
.timer1 =
new
System.Windows.Forms.Timer(
this
.components);
((System.ComponentModel.ISupportInitialize)(
this
.trackBar1)).BeginInit();
this
.SuspendLayout();
//
// button1
//
this
.button1.Location =
new
System.Drawing.Point(7, 254);
this
.button1.Name =
"button1"
;
this
.button1.Size =
new
System.Drawing.Size(75, 23);
this
.button1.TabIndex = 1;
this
.button1.Text =
"打开"
;
this
.button1.UseVisualStyleBackColor =
true
;
this
.button1.Click +=
new
System.EventHandler(
this
.button1_Click);
//
// button2
//
this
.button2.Location =
new
System.Drawing.Point(88, 254);
this
.button2.Name =
"button2"
;
this
.button2.Size =
new
System.Drawing.Size(75, 23);
this
.button2.TabIndex = 2;
this
.button2.Text =
"播放"
;
this
.button2.UseVisualStyleBackColor =
true
;
this
.button2.Click +=
new
System.EventHandler(
this
.button2_Click);
//
// button3
//
this
.button3.Location =
new
System.Drawing.Point(331, 254);
this
.button3.Name =
"button3"
;
this
.button3.Size =
new
System.Drawing.Size(75, 23);
this
.button3.TabIndex = 3;
this
.button3.Text =
"暂停"
;
this
.button3.UseVisualStyleBackColor =
true
;
this
.button3.Click +=
new
System.EventHandler(
this
.button3_Click);
//
// button4
//
this
.button4.Location =
new
System.Drawing.Point(412, 254);
this
.button4.Name =
"button4"
;
this
.button4.Size =
new
System.Drawing.Size(75, 23);
this
.button4.TabIndex = 4;
this
.button4.Text =
"停止"
;
this
.button4.UseVisualStyleBackColor =
true
;
this
.button4.Click +=
new
System.EventHandler(
this
.button4_Click);
//
// button5
//
this
.button5.Location =
new
System.Drawing.Point(169, 254);
this
.button5.Name =
"button5"
;
this
.button5.Size =
new
System.Drawing.Size(75, 23);
this
.button5.TabIndex = 5;
this
.button5.Text =
"<<快退"
;
this
.button5.UseVisualStyleBackColor =
true
;
this
.button5.Click +=
new
System.EventHandler(
this
.button5_Click);
//
// button6
//
this
.button6.Location =
new
System.Drawing.Point(250, 254);
this
.button6.Name =
"button6"
;
this
.button6.Size =
new
System.Drawing.Size(75, 23);
this
.button6.TabIndex = 5;
this
.button6.Text =
">>快进"
;
this
.button6.UseVisualStyleBackColor =
true
;
this
.button6.Click +=
new
System.EventHandler(
this
.button6_Click);
//
// trackBar1
//
this
.trackBar1.Cursor = System.Windows.Forms.Cursors.NoMoveHoriz;
this
.trackBar1.LargeChange = 1;
this
.trackBar1.Location =
new
System.Drawing.Point(1, 210);
this
.trackBar1.Maximum = 50;
this
.trackBar1.Name =
"trackBar1"
;
this
.trackBar1.Size =
new
System.Drawing.Size(494, 45);
this
.trackBar1.TabIndex = 6;
this
.trackBar1.TickStyle = System.Windows.Forms.TickStyle.Both;
this
.trackBar1.Scroll +=
new
System.EventHandler(
this
.trackBar1_Scroll);
//
// timer1
//
this
.timer1.Interval = 500;
this
.timer1.Tick +=
new
System.EventHandler(
this
.timer1_Tick);
//
// Form1
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 12F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(496, 280);
this
.Controls.Add(
this
.button6);
this
.Controls.Add(
this
.button5);
this
.Controls.Add(
this
.button4);
this
.Controls.Add(
this
.button3);
this
.Controls.Add(
this
.button2);
this
.Controls.Add(
this
.button1);
this
.Controls.Add(
this
.trackBar1);
this
.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this
.Name =
"Form1"
;
this
.Text =
"多媒体播放器"
;
((System.ComponentModel.ISupportInitialize)(
this
.trackBar1)).EndInit();
this
.ResumeLayout(
false
);
this
.PerformLayout();
}
#endregion
private
System.Windows.Forms.Button button1;
private
System.Windows.Forms.Button button2;
private
System.Windows.Forms.Button button3;
private
System.Windows.Forms.Button button4;
private
System.Windows.Forms.Button button5;
private
System.Windows.Forms.Button button6;
private
System.Windows.Forms.TrackBar trackBar1;
private
System.Windows.Forms.Timer timer1;
}
}
1.3
Program.cs
using
System;
using
System.Windows.Forms;
namespace
DirectShow
{
static
class
Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static
void
Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(
false
);
Application.Run(
new
Form1());
}
}
}
2、播放选定的文件
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
MediaApp
{
public
partial
class
Form1 : Form
{
private
const
int
WS_CHILD = 0x40000000;
private
const
int
WS_CLIPCHILDREN = 0x2000000;
private
QuartzTypeLib.IMediaControl MyControl =
null
;
private
QuartzTypeLib.IVideoWindow MyWindow =
null
;
public
Form1()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
OpenFileDialog MyDialog =
new
OpenFileDialog();
MyDialog.Filter =
"Media Files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|All Files|*.*"
;
if
(MyDialog.ShowDialog() == DialogResult.OK)
{
try
{
QuartzTypeLib.FilgraphManager MyManager =
new
QuartzTypeLib.FilgraphManager();
if
(MyControl !=
null
) MyControl.Stop();
//如果已经播放过,则先停止
MyManager.RenderFile(MyDialog.FileName);
//加载音频、视频文件
MyWindow = (QuartzTypeLib.IVideoWindow)MyManager;
try
//音频文件不需要用pictureBox1,所以会报错
{
MyWindow.Owner = (
int
)pictureBox1.Handle;
MyWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
MyWindow.SetWindowPosition(pictureBox1.ClientRectangle.Left,
pictureBox1.ClientRectangle.Top,pictureBox1.ClientRectangle.Width,pictureBox1.ClientRectangle.Height);
}
catch
{ }
MyControl = (QuartzTypeLib.IMediaControl)MyManager;
MyControl.Run();
}
catch
(Exception Mye)
{
MessageBox.Show(
this
,Mye.Message,
"信息提示"
,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}
}
2、播放指定的文件
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
WindowsFormsApplication1
{
public
partial
class
Form1 : Form
{
private
const
int
WS_CHILD = 0x40000000;
private
const
int
WS_CLIPCHILDREN = 0x2000000;
private
QuartzTypeLib.IMediaControl MyControl =
null
;
private
QuartzTypeLib.IVideoWindow MyWindow =
null
;
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
try
{
QuartzTypeLib.FilgraphManager MyManager =
new
QuartzTypeLib.FilgraphManager();
if
(MyControl !=
null
) MyControl.Stop();
//如果已经播放过,则先停止
MyManager.RenderFile(
"Ending.mpg"
);
//加载音频、视频文件
MyWindow = (QuartzTypeLib.IVideoWindow)MyManager;
try
//音频文件不需要用pictureBox1,所以会报错
{
MyWindow.Owner = (
int
)pictureBox1.Handle;
MyWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
MyWindow.SetWindowPosition(pictureBox1.ClientRectangle.Left,
pictureBox1.ClientRectangle.Top, pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
}
catch
{ }
MyControl = (QuartzTypeLib.IMediaControl)MyManager;
MyControl.Run();
}
catch
(Exception Mye)
{
MessageBox.Show(
this
, Mye.Message,
"信息提示"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
C#编写媒体播放器--Microsoft的Directx提供的DirectShow组件,该组件的程序集QuartzTypeLib.dll.的更多相关文章
- c#中用DirectShow实现媒体播放器的核心(1) DirectShow简介
用.net做多媒体开发的似乎不多,所以网上资源也少,看的人更少.不过我的博客上居然还有几位在等新文章的人,有点出乎我的意料了.目前我已不再从事多媒体相关的工作,加入新公司至今都忙到吐血,再加上害怕水平 ...
- WinForm媒体播放器
媒体播放控件(Windows Media Player )的常用属性和方法,并且利用它设计一个简单的媒体应用程序——媒体播放器.该媒体播放器可以播放 wav.avi.mid 和 mp3 等格式的文件. ...
- JavaScript自定义媒体播放器
使用<audio>和<video>元素的play()和pause()方法,可以手工控制媒体文件的播放.组合使用属性.事件和这两个方法,很容易创建一个自定义的媒体播放器,如下面的 ...
- (原创)jQuery Media Plugin-jQuery的网页媒体播放器插件的使用心得
jQuery Media Plugin是一款基于jQuery的网页媒体播放器插件,它支持大部分的网络多媒体播放器和多媒体格式,比如:Flash, Windows Media Player, Real ...
- Plyr – 简单,灵活的 HTML5 媒体播放器
Plyr 是一个简单的 HTML5 媒体播放器,包含自定义的控制选项和 WebVTT 字幕.它是只支持现代浏览器,轻量,方便和可定制的媒体播放器.还有的标题和屏幕阅读器的全面支持. 在线演示 ...
- 【C语言入门教程】4.10 综合实例 - 媒体播放器
4.10.1 建立播放列表 数据字典 名称 数据类型 说明 MAX_LENGTH 符号常量 用于定义数组长度,表示列表最大长度 MAX_FILE_LENGTH 符号常量 用于定义数组长度,表示文件名最 ...
- 快速构建Windows 8风格应用21-构建简单媒体播放器
原文:快速构建Windows 8风格应用21-构建简单媒体播放器 本篇博文主要介绍如何构建一个简单的媒体播放器. <快速构建Windows 8风格应用20-MediaElement>博文中 ...
- Movist for Mac(高清媒体播放器)v2.0.7中文特别版
Movist for Mac中文破解版是目前Mac平台上最好用的视频播放器,功能强大简单好用.movist mac版拥有美观简洁的用户界面,提供多种功能,支持视频解码加速高品质的字幕,全屏幕浏览,是与 ...
- VLC 媒体播放器
VLC 媒体播放器 VLC 媒体播放器是一个便携式. 免费.开源. 跨平台的媒体播放器. VideoLAN 项目的流式媒体服务器.分为Windows Phone版本和Android版本. 下载地址: ...
随机推荐
- Android--消除“Permission is only granted to system apps”错误
原文:http://blog.csdn.net/gaojinshan/article/details/14230673 在AndroidManifest.xml中使用了如下的配置: <uses- ...
- 如何在LINUX VPS上安装VPN详细步骤
在vps上安装vpn是个难点,很多朋友都不会,这儿结合一位朋友的安装经验做下介绍.注意你的vps要安装做vpn使用,一定要注意你的流量限制,使用Vpn上游戏上youtube看视频等都比较耗费资源,否则 ...
- ASP.NET Web - 回送
如果希望把更改事件立即传送给服务器,可以把AutoPostback属性设置为true.这样就会使用客户端的JavaScript把窗体数据立即提交给服务器.当然,网络通信量也会增加.使用这个功能时要小心 ...
- Eclipse 3.7(代号Indigo) 中文字体太小解决办法(转)
升级到3.7Eclipse最直观的反映就是,中文怎么那么小啊---- 相当不方便. 其实这是Eclipse的默认字体换了,以前的一直是Courier New,现在修改字体也找不到了,算了不找了. 这次 ...
- 学习Linux第一天
1.简介: 记住这个名字:Linus Torvals 系统组成:Linux内核,Shell, 文件系统,实时程序 Tips:在系统启动过程中,使用Alt+F2组合键,可以查看Ubuntu启动的详细过程 ...
- IntelliJ IDEA 14 创建maven项目二
前言: 在我的idea14使用maven创建web工程文章介绍了如何运用idea14创建maven项目--但有瑕疵,如下: 今天在群里交流才得知起因: 原来一直这样创建的--但结果都一样,均出现上面的 ...
- 网站常用css必备css reset
在我们写前端代码页面的时候,很多常用的CSS类都是固定的!但没有一个标准或者大家都按自己的方式去随意的写,这样就每次都重复写一些固定的类! 为此HTML5 Doctor(HTML5医生)为我们总结了一 ...
- Node.js 随记
http://nodejs.org/ 下载并安装 node.js 最新版本 运行cmd,输入npm install xxxxxx 回车,安装扩展模块,如:express,socket.io等 运行c ...
- [转载]C#中的WebBrowser控件的使用
http://www.cnblogs.com/txw1958/archive/2012/09/24/CSharp-WebBrowser.html
- Caffe训练好的网络对图像分类
对于训练好的Caffe 网络 输入:彩色or灰度图片 做minist 下手写识别分类,不能直接使用,需去除均值图像,同时将输入图像像素归一化到0-1直接即可. #include <caffe/c ...