如何在c#中播放来自dll的视频? [英] How to play the video from the dll in c#?
本文介绍了如何在c#中播放来自dll的视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(问题已更新)
我试图用c#播放视频。
我有dll包含播放视频的功能。我唯一需要的是当我通过在c#中给出视频路径名称来调用Renderfile函数时,视频必须在图片框中播放。
这是创建dll的c ++代码:
// RenderVideoFile.cpp:定义DLL应用程序的导出函数。
//
#include stdafx.h
#include RenderVideoFile.h
void RenderFile(WCHAR * strFilename,HWND hwnd)
{
IGraphBuilder * pGraph = NULL;
IMediaControl * pControl = NULL;
IMediaEvent * pEvent = NULL;
IVMRWindowlessControl * pWc = NULL;
// 初始化COM库。
HRESULT hr = S_OK; // = CoInitialize(NULL);
if (FAILED(hr))
{
printf( 错误 - 无法初始化COM库);
return ;
}
// 创建过滤器图形管理器并查询接口。
hr = CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,
IID_IGraphBuilder,( void **)& pGraph);
if (FAILED(hr))
{
printf( 错误 - 无法创建过滤器图形管理器。);
return ;
}
hr = pGraph-> QueryInterface(IID_IMediaControl,( void **)& pControl);
hr = pGraph-> QueryInterface(IID_IMediaEvent,( void **)& pEvent);
hr = InitWindowlessVMR(hwnd,pGraph,& pWc);
// 构建图表。重要信息:将此字符串更改为系统上的文件。
hr = pGraph-> RenderFile(strFilename,NULL);
long lWidth,lHeight;
hr = pWc-> GetNativeVideoSize(& lWidth,& lHeight,NULL,NULL);
if (SUCCEEDED(hr))
{
RECT rcSrc,rcDest;
// 设置源矩形。
SetRect(& rcSrc,< span class =code-digit> 0 , 0 ,lWidth,lHeight);
// 获取窗口客户区。
GetClientRect( HWND,&安培; rcDest);
// 设置目标矩形。
SetRect(& rcDest,< span class =code-digit> 0 , 0 ,rcDest.right,rcDest.bottom);
// 设置视频位置。
hr = pWc - > SetVideoPosition(& rcSrc,& rcDest);
}
if (SUCCEEDED(hr))
{
// 运行图表。
hr = pControl-> Run();
if (SUCCEEDED(hr))
{
// 等待完成。
long evCode;
pEvent-> WaitForCompletion(INFINITE,& evCode);
// 注意:不要在实际应用中使用INFINITE,因为它
// 可以无限期阻止。
}
}
pWc-> Release();
pControl-> Release();
pEvent-> Release();
pGraph-> Release();
// CoUninitialize();
}
这是C#代码:
namespace CSharpTest
{
public partial class Form1:Form
{
[DllImport( C:\\RenderVideoFile.dll,CharSet = CharSet.Unicode)]
public static extern void RenderFile(字符串strFilename, IntPtr hwnd);
RenderFile( C:\\ 0123.avi,pictureBox1.Handle) ;
}
}
我从dll中导入了c#中的renderfile函数。执行时没有任何错误,但视频没有播放。我必须纠正什么!?
解决方案
[DllImport( C:\\Documents and Settings \\BTC \\CSharpTest \\ RenderFileDll.dll,CharSet = CharSet.Ansi)]
public static extern void RenderFile( string strFilename);
不是使用编组概念从dll中表示和导入函数,而是按照我的预期工作:(视频使用下面的代码在图片框中播放)
此链接将有用工作原理:
如何制作C ++类 [ ^ ]
namespace CSharpManaged
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)
{
CSUnmanagedTestClass testClass = new CSUnmanagedTestClass();
testClass.RenderFile( C:\\123.avi,pictureBox1。处理);
}
}
}
没有你不能像这样播放视频..你可以使用html5视频标签播放视频
< video width = 320 height = 240 controls < span class =code-keyword> = >
< source src = http://www.w3schoo ls.com/html/movie.mp4\" type = video / mp4 >
< source src = http://www.w3schools.com/html/movie.ogg type = video / ogg >
您的浏览器不支持视频标记。
< / video > 跨度>
(The Question was updated)
Am trying to play the video in c#.
I have the dll which is containing the function to play the video. The only thing i need is that the video has to play in the picture box when i call the Renderfile function by giving the video path name in c#.
Here is the c++ code which was created the dll:
// RenderVideoFile.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "RenderVideoFile.h"
void RenderFile(WCHAR *strFilename,HWND hwnd)
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
IVMRWindowlessControl *pWc=NULL;
// Initialize the COM library.
HRESULT hr=S_OK;// = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return;
}
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
return;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
hr = InitWindowlessVMR(hwnd, pGraph, &pWc);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(strFilename, NULL);
long lWidth, lHeight;
hr = pWc->GetNativeVideoSize(&lWidth, &lHeight, NULL, NULL);
if (SUCCEEDED(hr))
{
RECT rcSrc, rcDest;
// Set the source rectangle.
SetRect(&rcSrc, 0, 0, lWidth, lHeight);
// Get the window client area.
GetClientRect(hwnd,&rcDest);
// Set the destination rectangle.
SetRect(&rcDest, 0, 0, rcDest.right, rcDest.bottom);
// Set the video position.
hr = pWc->SetVideoPosition(&rcSrc, &rcDest);
}
if (SUCCEEDED(hr))
{
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
}
pWc->Release();
pControl->Release();
pEvent->Release();
pGraph->Release();
//CoUninitialize();
}
This is the C# code:
namespace CSharpTest
{
public partial class Form1 : Form
{
[DllImport("C:\\RenderVideoFile.dll",CharSet=CharSet.Unicode)]
public static extern void RenderFile(string strFilename,IntPtr hwnd);
RenderFile("C:\\123.avi",pictureBox1.Handle);
}
}
I imported the function of renderfile in c# from the dll. It was executed without any error but the video was not played. What i have to correct!?
解决方案
[DllImport("C:\\Documents and Settings\\BTC\\CSharpTest\\RenderFileDll.dll", CharSet = CharSet.Ansi)] public static extern void RenderFile(string strFilename);
Rather than expoting and importing the functions from the dll using the marshal concept it works as i expected:(The video was played in the picture box using the below code)
This link will be useful to know how it works:
How to Marshal a C++ Class[^]
namespace CSharpManaged { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { CSUnmanagedTestClass testClass = new CSUnmanagedTestClass(); testClass.RenderFile("C:\\123.avi", pictureBox1.Handle); } } }
no you cant play video like this ..you can use html5 video tag to play video <video width="320" height="240" controls=""> <source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4"> <source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg"> Your browser does not support the video tag. </video>
这篇关于如何在c#中播放来自dll的视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文