很多人第一次接触到DirectX大都是通过游戏,至于安装、升级DirectX的原因无非是满足游戏运行的需要。Direct X其实是微软公司推出的一个为Windows平台的多媒体API函数库,它提供标准接口来与显卡和声卡、输入设备等进行交互。如果没有这组标准API函数 库,那你就需要为每一种显卡、声卡的每个组合和每种类型的键盘、鼠标和游戏杆编写不同的代码。这不又回到了以前的Dos时代。为了解决这个问题,微软公司推出了DirectX。DirectX从具体的硬件中抽象出来,并且将一组通用指令转换成硬件的具体命令。这样开发语言通过调用统一标准的Direct X函数库就可以操作每一种显卡、声卡的每个组合和每种类型的键盘、鼠标和游戏杆等多媒体了。
表01:Direct X SDK9.0中的定义的命名空间及其主要的作用 Direct X内容十分丰富,下文介绍的只是其中的一个小的应用,即用Visual C#调用Direct X中的SDK来播放视频文件为例子,程序中只使用了命名空间"Microsoft.DirectX.AudioVideoPlayback"。 Microsoft.DirectX.AudioVideoPlayback命名空间中定义了三个类:"Audio"、"Video" 和"TextureRenderEventArgs"。其中前二个类是最常用的,分别用来支持音频和视频。 Microsoft.DirectX.AudioVideoPlayback命名空间中定义的常用属性及其具体的作用可参阅表02所示:
表02:Microsoft.DirectX.AudioVideoPlayback命名空间中定义的常用属性及其具体的作用 Microsoft.DirectX.AudioVideoPlayback命名空间中定义的常用方法及其具体的作用可参阅表03所示:
表03:Microsoft.DirectX.AudioVideoPlayback命名空间中定义的常用属性及其具体的作用 了解了以上这些基础知识后,下面就开始介绍Visual C#调用Direct X播放视频文件的具体实现方法。 二.本文章的程序设计、调试和运行的环境: (1).微软公司视窗2003中文企业版。 (2).Visual Studio .Net 2003企业构建版,.Net FrameWork SDK 1.1版本号4322。 Visual C#使用Direct X的实现视频播放难点无非以下几点: 1. 掌握在Visual C#引入Dirext X函数库的方法。 2. 设置Direct X视频播放的宿主组件。 3. 基本的播放命令的设置:播放、停止、暂停等。 下面就是Visual C#使用Direct X的实现视频播放具体实现步骤: 1. 启动Visual Studio .Net。 2. 选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框。 3. 将【项目类型】设置为【Visual C#项目】。 4. 将【模板】设置为【Windows应用程序】。 5. 在【名称】文本框中输入【Visual C#中使用DriectX实现媒体播放】。 6. 在【位置】的文本框中输入【E:\VS.NET项目】,然后单击【确定】按钮。这样在"E:\VS.NET项目"目录中就创建了一个名称为"Visual C#中使用DriectX实现媒体播放"的文件夹,里面存放的就是"Visual C#中使用DriectX实现媒体播放"项目的所有文件。 7. 选中【解决方案管理器】|【引用】,单击鼠标右键,选中【添加引用】菜单,具体如图01所示: 图01:添加引用 8. 选中【添加引用】菜单后,弹出【添加引用】对话框,按照图02所示,在【选定的组件】栏目中加 入"Microsoft.DirectX.AudioVideoPlayback"后,单击【确定】按钮,则引 用"Microsoft.DirectX.AudioVideoPlayback"文件成功。这是因为Visual Studio .Net的缺省编译环境中没有"Microsoft.DirectX.AudioVideoPlayback.dll"文件,而程序中却使用 到"Microsoft.DirectX.AudioVideoPlayback"命名空间,所以要在编译环境中引 用"Microsoft.DirectX.AudioVideoPlayback"所在的文件。 9. 把Visual Studio .Net的当前窗口切换到【Form1.cs(设计)】窗口,并从【工具箱】中的【Windows窗体组件】选项卡中往设计窗体中拖入下列组件,并执行相应操作: 一个OpenFileDialog组件,用于选择播放的视频;一个panel组件,作为视频播放的宿主组件;四个Button按钮,分别执行视频打开、 播放、暂停和停止操作。并在这四个组件拖入设计窗口后分别双击它们,则系统会在Form1.cs中分别产生这四个组件Click事件对应的处理代码。 10. 把Visual Studio .Net的当前窗口切换到Form1.cs的代码编辑窗口,在Form1.cs的首部的引入命名空间的代码区中,用下列代码替换Form1.cs中由系统自动产生的引入命名空间代码: using System ;
using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using Microsoft.DirectX.AudioVideoPlayback ; //引入视频播放所要使用的Direct X命名空间 11. 在Form1.cs的class的代码区中添加下列代码,下列代码是创建全局使用的Video实例: private Video MyVideo = null ;
//创建一个Video实例 12. 以下面代码替代系统产生的InitializeComponent过程。下面代码是对加入窗体的组件以及创建的全局变量进行初始化和定义四个Button组件的Click事件和Form1的Load事件: private void InitializeComponent ( )
{ this.panel1 = new System.Windows.Forms.Panel ( ) ; 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.openFileDialog1 = new System.Windows.Forms.OpenFileDialog ( ) ; this.SuspendLayout ( ) ; this.panel1.Dock = System.Windows.Forms.DockStyle.Top ; this.panel1.Location = new System.Drawing.Point ( 0, 0 ) ; this.panel1.Name = "panel1" ; this.panel1.Size = new System.Drawing.Size ( 540, 346 ) ; this.panel1.TabIndex = 0 ; this.button1.Location = new System.Drawing.Point ( 62, 380 ) ; this.button1.Name = "button1" ; this.button1.Size = new System.Drawing.Size ( 80, 38 ) ; this.button1.TabIndex = 1 ; this.button1.Text = "打开" ; this.button1.Click += new System.EventHandler ( this.button1_Click ) ; this.button2.Location = new System.Drawing.Point ( 165, 380 ) ; this.button2.Name = "button2" ; this.button2.Size = new System.Drawing.Size ( 81, 38 ) ; this.button2.TabIndex = 1 ; this.button2.Text = "播放" ; this.button2.Click += new System.EventHandler ( this.button2_Click ) ; this.button3.Location = new System.Drawing.Point ( 268, 380 ) ; this.button3.Name = "button3" ; this.button3.Size = new System.Drawing.Size ( 80, 38 ) ; this.button3.TabIndex = 1 ; this.button3.Text = "暂停" ; this.button3.Click += new System.EventHandler ( this.button3_Click ) ; this.button4.Location = new System.Drawing.Point ( 371, 380 ) ; this.button4.Name = "button4" ; this.button4.Size = new System.Drawing.Size ( 81, 38 ) ; this.button4.TabIndex = 1 ; this.button4.Text = "停止" ; this.button4.Click += new System.EventHandler ( this.button4_Click ) ; this.openFileDialog1.DefaultExt = "avi" ; this.openFileDialog1.Filter = "视频文件|*.avi||" ; this.openFileDialog1.Title = "请选择播放的AVI文件" ; this.AutoScaleBaseSize = new System.Drawing.Size ( 6, 14 ) ; this.ClientSize = new System.Drawing.Size ( 540, 461 ) ; this.Controls.Add ( this.button1 ) ; this.Controls.Add ( this.panel1 ) ; this.Controls.Add ( this.button2 ) ; this.Controls.Add ( this.button3 ) ; this.Controls.Add ( this.button4 ) ; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog ; this.MaximizeBox = false ; this.Name = "Form1" ; this.Text = "Visual C#中使用DriectX实现媒体播放" ; this.Load += new System.EventHandler ( this.Form1_Load ) ; this.ResumeLayout ( false ) ; } 至此【Visual C#中使用DriectX实现媒体播放】项目的界面设计和功能实现的前期工作就完成了,设计界面如图03所示: 图03:【Visual C#中使用DriectX实现媒体播放】项目的界面设计 13. 用下列代码替换Form1.cs中的button1组件的Click事件对应的处理代码,下列代码的功能是打开选定的视频文件,并在定义的Panel组件上显示开始第一帧: private void button1_Click ( object sender, System.EventArgs e )
{ openFileDialog1.InitialDirectory = Application.StartupPath ; if ( openFileDialog1.ShowDialog ( ) == DialogResult.OK ) { // 记录panel组件的大小 int height = panel1.Height ; int width = panel1.Width ; // 如果存在打开的Video文件,释放它 if ( MyVideo != null ) { MyVideo.Dispose ( ) ; } // 打开一个新的Video文件 MyVideo = new Video ( openFileDialog1.FileName ) ; // 把Video文件分配给创建的Panel组件 MyVideo.Owner = panel1 ; // 以记录的panel组件的大小来重新定义 panel1.Width = width ; panel1.Height = height ; // 播放AVI文件的第一帧,主要是为了在panel中显示 MyVideo.Play ( ) ; MyVideo.Pause ( ) ; } //确定窗体中的各按钮状态 if ( MyVideo == null ) { button2.Enabled = false ; button3.Enabled = false ; button4.Enabled = false ; } else { button2.Enabled = true ; button3.Enabled = true ; button4.Enabled = true ; } } 14. 用下列代码替换Form1.cs中的button2组件的Click事件对应的处理代码,下列代码的功能播放当前打开的视频文件: private void button2_Click ( object sender, System.EventArgs e )
{ if ( MyVideo != null ) { MyVideo.Play ( ) ; } } 15. 用下列代码替换Form1.cs中的button3组件的Click事件对应的处理代码,下列代码的功能暂停播放当前打开的视频文件: private void button3_Click ( object sender, System.EventArgs e )
{ if ( MyVideo != null ) { MyVideo.Pause ( ) ; } } 16. 用下列代码替换Form1.cs中的button4组件的Click事件对应的处理代码,下列代码的功能停止播放当前打开的视频文件: private void button4_Click ( object sender, System.EventArgs e )
{ if ( MyVideo != null ) { MyVideo.Stop ( ) ; } } 17. 在button4的Click事件之后,添加下列代码,下列代码的功能是初始化Form窗体中的button组件: //初始化窗体中各按钮的状态
private void Form1_Load ( object sender, System.EventArgs e ) { if ( MyVideo == null ) { button2.Enabled = false ; button3.Enabled = false ; button4.Enabled = false ; } else { button2.Enabled = true ; button3.Enabled = true ; button4.Enabled = true ; } } 18. 至此,在上述步骤都正确完成,并全部保存后,【Visual C#中使用DriectX实现媒体播放】项目的全部工作就完成了。此时单击快捷键【F5】运行程序后,图04是播放视频文件时得到的程序运行界面: 图04:【Visual C#中使用DriectX实现媒体播放】项目的运行界面 四.总结: Direct X的出现的确解决了程序员的很多底层的工作,把面对各种烦杂硬件的工作交给了Direct X了。虽然Direct X从非托管版本发展到现在的托管版本,争论一直存在,但越来越多的开发商把自己的软件用托管的Direct X或者结合使用托管和非托管Direct X开发,也从另外一个方面证明了托管的Direct X的生命力。本文结合一个示例具体介绍Visual C#调用非托管Direct X的方法实现视频播放,这对于托管Direct X来说只是其中的一个小应用。最后希望此篇文章对那些希望了解、掌握在Visual C#调用Direct X编写游戏程序的朋友有所帮助。 (秩名) |