C#如何使用DirectShow(quartz.dll)从内存流播放视频? [英] C# How Can I Play A Video From A Memory Stream Using DirectShow(quartz.dll)?
问题描述
我有一个使用QuartzTypeLib(quartz.dll)播放视频的C#Visual Studio WinForms .NET应用程序。使用我编写的代码,我可以播放硬盘中的任何视频文件。
I have a C# Visual Studio WinForms .NET app that plays video using the QuartzTypeLib (quartz.dll). With the code I've written, I can play any video file from the hard drive.
以下是应用启动时执行的顶部代码:
Here's the code at the top that executes when the app starts:
public const int WS_CHILD = 0x40000000;
public const int WS_CLIPCHILDREN = 0x2000000;
public QuartzTypeLib.IMediaControl mc;
public QuartzTypeLib.IVideoWindow videoWindow = null;
IMediaPosition mp = null;
这是打开视频文件的代码:
And here's the code that opens the video file:
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甚至可以实现,如果可以,我需要添加什么代码并将其放在哪里?
Notice that I have code that converts the video to a Base64 string. This string will obviously have to be loaded into a memory stream. I'd like to add code that will allow me to play a video from a memory stream. Is that even possible with DirectShow and if so, what code would I need to add and where would I put it?
推荐答案
DirectShow方法是创建一个特殊的所谓的过滤器(源过滤器),该过滤器输出视频数据,然后将其添加到图形链。
The DirectShow way is to create a special so-called filter (source filter) that outputs video data and then add it to the graph chain.
通常,过滤器是用C ++编写的。当然,几乎所有可以用C ++编写的代码都可以用C#重写。可能需要大量工作,例如看这篇文章:
Usually a filter is written in C++. Of course almost any code that can be written in C++ can be rewritten in C#. It could take a lot of work, for instance look at this article:
http://www.codeproject.com/Articles/421167/Pure-NET-DirectShow-Filters-in-Csharp
另一种方法是文件仿真。在这种情况下,您将需要BoxedApp之类的第三方解决方案。
Another way is file emulation. In this case you would need 3rd party solution like BoxedApp.
其思想是拦截某些文件功能(如SetFilePointer和ReadFile),以提供从真实位置写入的数据文件(但实际上是从内存中读取的)。
The idea is to intercept some file functions like SetFilePointer and ReadFile, to provide data as it is written from a real file (but in fact it is read from memory).
这篇关于C#如何使用DirectShow(quartz.dll)从内存流播放视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!