命名空间 | 描述 |
Microsoft.DirectX | 公共类和数学结构 |
Microsoft.DirectX.Direct3D | 3D图形和助手库 |
Microsoft.DirectX.DirectDraw | Direct Draw 图形API。这是旧式命名空间,现在已经不需要使用它。 |
Microsoft.DirectX.DirectPlay | 用于多玩家游戏的网络API |
Microsoft.DirectX.DirectSound | 声音支持 |
Microsoft.DirectX.DirectInput | 输入设备支持(例如,鼠标和游戏杆) |
Microsoft.DirectX.AudioVideoPlayback | 播放视频和音频(例如,在电脑上播放各自视频动画文件) |
Microsoft.DirectX.Diagnostics | 疑难解答 |
Microsoft.DirectX.Security | 访问安全性 |
Microsoft.DirectX.Security.Permissions | 访问安全权限 |
属性 | 描述 |
Audio | 获取视频文件中的音频对象,可用来后续的音频播放 |
Caption | 获取或设置在Form上播放视频的名称 |
CurrentPosition | 获取或设置播放视频的当前位置 |
DefaultSize | 获取播放视频的缺省的视频大小 |
Fullscreen | 获取或设置视频文件是否在全屏模式下播放 |
IsCursorHidden | 获取播放的视频时鼠标的状态:隐藏或显示 |
Owner | 获取或设置视频播放的宿主组件 |
Paused | 获取当前的播放状态是否处于暂停状态 |
Playing | 获取当前的播放状态是否处于播放状态. |
SeekingCaps | 获取是否可以搜索性能 |
Size | Retrieves and sets the size of the video for playback. |
State | 获取当前的播放状态 |
Stopped | 获取当前的播放状态是否处于停止状态 |
StopPosition | 获取播放的视频的停止播放位置 |
方法 | 描述 |
HideCursor | 隐藏当前播放视频的鼠标 |
Open | 装入新的文件到Video对象中 |
Pause | 设置为暂停播放状态. |
Play | 设置为播放状态 |
SeekCurrentPosition | 搜索转入到制定的播放位置 |
SeekStopPosition | 设置一个新的停止位置 |
ShowCursor | 显示当前播放视频的鼠标 |
Stop | 设置为停止播放状态 |
Video | 初始化一个新的Video实例 |
图01:添加引用 |
8. 选中【添加引用】菜单后,弹出【添加引用】对话框,按照图02所示,在【选定的组件】栏目中加入"Microsoft.DirectX.AudioVideoPlayback"后,单击【确定】按钮,则引用"Microsoft.DirectX.AudioVideoPlayback"文件成功。这是因为Visual Studio .Net的缺省编译环境中没有"Microsoft.DirectX.AudioVideoPlayback.dll"文件,而程序中却使用到"Microsoft.DirectX.AudioVideoPlayback"命名空间,所以要在编译环境中引用"Microsoft.DirectX.AudioVideoPlayback"所在的文件。
using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using Microsoft.DirectX.AudioVideoPlayback ; //引入视频播放所要使用的Direct X命名空间 |
private Video MyVideo = null ; //创建一个Video实例 |
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 ) ; } |
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 ; } } |
private void button2_Click ( object sender, System.EventArgs e ) { if ( MyVideo != null ) { MyVideo.Play ( ) ; } } |
private void button3_Click ( object sender, System.EventArgs e ) { if ( MyVideo != null ) { MyVideo.Pause ( ) ; } } |
private void button4_Click ( object sender, System.EventArgs e ) { if ( MyVideo != null ) { MyVideo.Stop ( ) ; } } |
//初始化窗体中各按钮的状态 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 ; } } |