工程类型:对话框
在类成员中定义以下接口:
IGraphBuilder* m_pGraph;
IMediaControl* m_pControl;
IVideoWindow* m_pVideo;
在OnInitDialog()函数中写:
// 实例化IID_IGraphBuilder接口
HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL,
CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&m_pGraph);
if(FAILED(hr))
{
TRACE0("Create IGraphBuilder failed./n");
return FALSE;
}
// 获得媒体控制接口
hr = m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pControl);
if(FAILED(hr))
{
TRACE0("Failed to query IID_IMediaControl interface/n");
return FALSE;
}
// 设置媒体文件路径
hr = m_pGraph->RenderFile(L"E://music//214love.asf", NULL);
if(FAILED(hr))
{
TRACE0("RenderFile Failed/n");
return FALSE;
}
// 获得视频窗口接口
hr = m_pGraph->QueryInterface(IID_IVideoWindow, (void**)&m_pVideo);
if(FAILED(hr))
{
TRACE0("Failed to query IID_IVideoWindow interface/n");
return FALSE;
}
// 设置视频窗口
m_pVideo->put_Owner((OAHWND)GetSafeHwnd());
m_pVideo->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);
m_pVideo->SetWindowPosition(0, 0, 320, 240);
m_pVideo->put_Visible(OATRUE);
m_pControl->Run(); // 开如播放
注意:关闭主窗口时记得释放这些接口