图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的当前窗口切换到窗口,并从中的选项卡中往设计窗体中拖入下列组件,并执行相应操作: 一个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 ) ;}
至此项目的界面设计和功能实现的前期工作就完成了,设计界面如图03所示:
图03:项目的界面设计
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 ; }}