繁体   English   中英

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

[英]C# How Can I Play A Video From A Memory Stream Using DirectShow(quartz.dll)?

我有一个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甚至可以实现吗,如果可以,我需要添加什么代码并将其放置在哪里?

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

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

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

另一种方法是文件仿真。 在这种情况下,您将需要BoxedApp之类的第三方解决方案。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM