Wu.Country@侠缘

勤学似春起之苗,不见其增,日有所长; 辍学如磨刀之石,不见其损,日所有亏!

小记一下DirectX里的Video和Audio

原本是DX里的一个例子,我觉得还可以,就COPY出来,想改改看。结果让我大失所望。
最简单要求,就是对音频文件和视频文件可以定位查找,本来它的Video和Audio类都提供了Seek方法,但它们根本不能工作,每次Seek后,都回到0的位置上。后来用Reflector来看它的源代码,结果也是不尽人意,很多底层的代码没有办法反射出来。特别是DX里的一些接口,根本无法查看。最后也只好先暂停了。
本来还想着,要是DX在.Net下封装的比较好的话,我就可以少用C++的COM组件了,直接用C#来写组件也方便多了。没想到MS的托管DX确实还不够完善,我看可能要等以后的版本了。不过它的MediaPlay的SDK还是不错的,有机会也看看。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.AudioVideoPlayback;

namespace Webb.DirectX.Debug2
{
    
/// <summary>
    
/// Summary description for Form1.
    
/// </summary>

    public class AVPlayer : System.Windows.Forms.Form
    
{

        
private string filterText = "Video Files (*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv)|*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv|" +
            
"Audio files (*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma)|*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma|" +
            
"MIDI Files (*.mid, *.midi, *.rmi)|*.mid; *.midi; *.rmi|" +
            
"Image Files (*.jpg, *.bmp, *.gif, *.tga)|*.jpg; *.bmp; *.gif; *.tga|" +
            
"All Files (*.*)|*.*";

        
private Video ourVideo = null;
        
private Audio ourAudio = null;

        
Winforms variables

        
public AVPlayer()
        
{
            
//
            
// Required for Windows Form Designer support
            
//
            InitializeComponent();

            OpenFile();
        }


        
private bool _IsVideo = false;

        
/// <summary>
        
/// Clean up any resources being used.
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            CleanupObjects();
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows Form Designer generated code

        
private void CleanupObjects()
        
{
            
if (ourVideo != null)
                ourVideo.Dispose();
            
if (ourAudio != null)
                ourAudio.Dispose();
        }


        
private void OpenFile()
        
{
//            if ((ofdOpen.InitialDirectory == null) || (ofdOpen.InitialDirectory == string.Empty))
//                ofdOpen.InitialDirectory = DXUtil.SdkMediaPath;

            ofdOpen.Filter 
= filterText;
            ofdOpen.Title 
= "Open media file";
            ofdOpen.ShowDialog(
this);

            
// Now let's try to open this file
            if ((ofdOpen.FileName != null&& (ofdOpen.FileName != string.Empty))
            
{
                
try
                
{
                    
if (ourVideo == null)
                    
{
                        
// First try to open this as a video file
                        ourVideo = new Video(ofdOpen.FileName);
                        ourVideo.Ending 
+= new System.EventHandler(this.ClipEnded);
                        ourVideo.Owner 
= this;
                        
// Start playing now
                        ourVideo.Play();
                        
this._IsVideo = true;
                    }

              &n