非常简单的视频播放器源码

#include "dshow.h"

//链接库
#pragma comment(lib,"strmiids.lib")

void main()
{
	//定义变量
	IGraphBuilder *pGraph;
	IMediaEvent *pEvent;
	IMediaControl *pControl;
	//初始化COM
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		printf("Initialize COM Failed!\n");
	}
	else
	{
		printf("Initialize COM successful!\n");
	}
	//创建Filter Graph Manager
	hr = CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,IID_IGraphBuilder, (void **)&pGraph);
	if (FAILED(hr))
	{
		printf("Create IGraphBuilder Failed!\n");
	}
	else
	{
		printf("Create IGraphBuilder successful!\n");
	}
	//查询接口
	hr = NOERROR;
	hr |= pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
	hr |= pGraph->QueryInterface(IID_IMediaEvent,(void **)&pEvent);
	if (FAILED(hr))
	{
		printf("QueryInterface Failed!\n");
	}
	else
	{
		printf("QueryInterface successful!\n");
	}

	//方法1(Rendder):提供输出Pin,自动加入必要的Filter,完成Filter Graph 构建
	IBaseFilter* pFilter;
	pGraph->AddSourceFilter(L"G:\\QQMiniDL\\Amtr.Hme.AnlFstngp.avi",L"test",&pFilter);
	IPin * pPin;
	IEnumPins *pEnum = NULL;
	pFilter->EnumPins(&pEnum);
	while(pEnum->Next(1,&pPin,NULL)==S_OK)
	{
		PIN_DIRECTION PinDir;
		pPin->QueryDirection(&PinDir);

		if(PinDir == PINDIR_OUTPUT)
		{
			IPin *pTemp;
			if(SUCCEEDED(pPin->ConnectedTo(&pTemp)))
			{
				pTemp->Release();
			}
			else
			{
				pGraph->Render(pPin); 
			}
		}
	}
	//方法2(RendderFile):提供原文件名,自动加入必要的Filter,完成Filter Graph 构建
	//hr = pGraph->RenderFile(L"G:\\QQMiniDL\\Amtr.Hme.AnlFstngp.avi",NULL);

	if (SUCCEEDED(hr))
	{
		pControl->Run();

		long evcode;
		pEvent->WaitForCompletion(INFINITE,&evcode);
	}
	//释放资源
	pEvent->Release();
	pControl->Release();
	pGraph->Release();
	CoUninitialize();
}

你可能感兴趣的:(DirectShow)