C#如何使用DirectShow(quartz.dll)从内存流中播放视频?

时间:2015-03-04 00:32:29

标签: c# video directshow

我有一个C#Visual Studio WinForms .NET应用程序,它使用QuartzTypeLib(quartz.dll)播放视频。使用我编写的代码,我可以播放硬盘中的任何视频文件。

这是应用程序启动时执行的顶部代码:

    public const int WS_CHILD = 0x40000000;
    public const int WS_CLIPCHILDREN = 0x2000000;
    public QuartzTypeLib.IMediaControl mc;
    public QuartzTypeLib.IVideoWindow videoWindow = null;
    IMediaPosition mp = null;

这是打开视频文件的代码:

    private void openMediaToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // Open a media file.
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Video Files|*.mpg;*.avi;*;*.wmv;*.mov";
        ofd.FilterIndex = 1;
        if (DialogResult.OK == ofd.ShowDialog())
        { 
            // Stop the playback for the current movie if a video is currently playing.
            if (mc != null)
                mc.Stop();
            if (pbVideoDisplay.Image != null)
                pbVideoDisplay.Image = null;
            // Load the movie file.
            FilgraphManager graphManager = new FilgraphManager();
            graphManager.RenderFile(ofd.FileName);
            mp = graphManager as IMediaPosition;
            mc = (IMediaControl)graphManager;
            tsbtnPlay.Enabled = tsbtnPause.Enabled = tsbtnStop.Enabled = true;

            // Attach the view to the picture box (pbVideoDisplay) on frmMain.
            try
            {
                videoWindow = (IVideoWindow)graphManager;
                videoWindow.Owner = (int)pbVideoDisplay.Handle;
                videoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
                videoWindow.SetWindowPosition(
                pbVideoDisplay.ClientRectangle.Left,
                pbVideoDisplay.ClientRectangle.Top,
                pbVideoDisplay.ClientRectangle.Width,
                pbVideoDisplay.ClientRectangle.Height);
            }
            catch //(Exception Ex)
            {
                // I'll write code for this when I have a need to.
            }
            // Now we convert the video to a byte array.
            FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
            try
            {
                // Here we convert the video to Base 64.
                VideoInBytes = new byte[fs.Length];
                VideoInBytes = System.Text.Encoding.UTF8.GetBytes(ofd.FileName);
                VideoInBase64 = Convert.ToBase64String(VideoInBytes);
            }
            catch //(Exception Ex)
            {
                //throw new Exception("Error in base64Encode" + Ex.Message);
            }
        }
    }

请注意,我有将视频转换为Base64字符串的代码。显然必须将此字符串加载到内存流中。我想添加允许我从内存流播放视频的代码。甚至可以使用DirectShow,如果是这样,我需要添加什么代码以及我将它放在哪里?

1 个答案:

答案 0 :(得分:2)

DirectShow方法是创建一个特殊的所谓过滤器(源过滤器),输出视频数据,然后将其添加到图形链中。

通常过滤器是用C ++编写的。当然,几乎所有可以用C ++编写的代码都可以用C#重写。这可能需要很多工作,例如看看这篇文章:

http://www.codeproject.com/Articles/421167/Pure-NET-DirectShow-Filters-in-Csharp

另一种方式是文件模拟。在这种情况下,您需要第三方解决方案,如BoxedApp。

这个想法是拦截一些文件函数,如SetFilePointer和ReadFile,以提供从真实文件写入的数据(但事实上它是从内存中读取的)。