簡體   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