获取directshow中的播放事件IMediaEvent
qq:9611153 微信lgs9611153
时间:2013/2/16
背景原因:
使用简单的代码,编写一个播放器,莫过于使用directshow直接播放了,这里讲一下在directshow的graph中添加事件处理的方法。
问题相关:
首先看一下directshow经典例程,经典的步骤:
如何添加播放事件:
声明和获取:
#define WM_GRAPHNOTIFY WM_APP + 1
IMediaEventEx *g_pEvent = NULL;
pGraph->QueryInterface(IID_IMediaEventEx, (void **)&g_pEvent);
g_pEvent->SetNotifyWindow((OAHWND)g_hwnd, WM_GRAPHNOTIFY, 0);//绑定到接收窗口
//In your application's WindowProc function, add a case statement for the WM_GRAPHNOTIFY message:
case WM_GRAPHNOTIFY:
HandleGraphEvent(); break;//在窗口消息处理函数中处理directshow事件
查询处理:
void HandleGraphEvent()
{
// Disregard if we don't have an IMediaEventEx pointer.
if (g_pEvent == NULL) return;
// Get all the events
long evCode, param1, param2;
HRESULT hr;
while (SUCCEEDED(g_pEvent->GetEvent(&evCode, ¶m1, ¶m2, 0)))//循环查询事件
{
g_pEvent->FreeEventParams(evCode, param1, param2);
switch (evCode)
{
case EC_COMPLETE: // Fall through.
case EC_USERABORT: // Fall through.
case EC_ERRORABORT:
CleanUp();
PostQuitMessage(0);
return;
}
}
}
接口释放:
// Disable event notification before releasing the graph.
g_pEvent->SetNotifyWindow(NULL, 0, 0);
g_pEvent->Release();
g_pEvent = NULL;