借助DirectX 9 播放AVI文件

首先在winfrom工程里引入

using Microsoft.DirectX.AudioVideoPlayback;

再申明一个:

 Video _video;

然后添加如下代码(省去其他控件申明)

void OpenVideo()
2        {
3            openFileDialog1.InitialDirectory = Application.StartupPath;
4            if (openFileDialog1.ShowDialog() == DialogResult.OK)
5            {
6                // open the video
7
8                //videoPanel 是 System.Windows.Forms.Panel控件,用来控制播放界面大小
9                int height = videoPanel.Height;
10                int width = videoPanel.Width;
11
12
13
14// dispose 清除上次播放的废弃资源
15              if (_video != null)
16              {
17                 _video.Dispose();
18              }

19
20                // 打开一个新的video播放
21                _video = new Video(openFileDialog1.FileName);
22
23                // 告诉程序将由videoPanel来包含_video
24                _video.Owner = videoPanel;
25
26                // 设置videoPanel的大小
27                videoPanel.Width = width;
28                videoPanel.Height = height;
29
30                // play
31                _video.Play();
32               
33            }

34
35      
38        }

39

Microsoft.DirectX.AudioVideoPlayback让播放avi很方便,这样不用控件也能坐出自己的播放器。

你可能感兴趣的:(借助DirectX 9 播放AVI文件)