aplayer:等待时自己写一个 GetMessage 循环,直等到 APlayer 的打开成功事件,获取到时长后,退出该循环即可。
(2017-04-25 22:38)
谢谢回复。和我想的一样。
BOOL CAPlayerDemoDlg::SyncWaitEventAndMessage(HANDLE hEventTarget)
{
if (hEventTarget == NULL)
return FALSE;
BOOL fQuit = FALSE; // should the loop terminate?
while (!fQuit)
{
HANDLE hEvent = hEventTarget;
//Wake when the kernel object is signaled OR if we have to process a UI message.
DWORD dwResult = ::MsgWaitForMultipleObjectsEx(1, &hEvent, INFINITE, QS_ALLEVENTS, MWMO_INPUTAVAILABLE);
switch (dwResult)
{
case WAIT_OBJECT_0: // The event became signaled.
fQuit = TRUE;
break;
case WAIT_OBJECT_0+1: // A message is in our queue;
// Dispatch all of the messages.
MSG msg;
//while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
while (::GetMessage(&msg, NULL, 0, 0))
{
if(msg.message == WM_QUIT)
{
// A WM_QUIT message. exit the loop
fQuit = TRUE;
}
else
{
// Translate and dispatch the message;
::TranslateMessage(&msg);
DispatchMessage(&msg);
}
}// Our queue is empty/
break;
default:
break;
}
}// End of while loop.
return fQuit;
}
double CAPlayerDemoDlg::startPlayback(const char * filename, long hwnd, bool bPlayAudio)
{
if(filename == NULL)
return -1;
if(!m_bInit || m_MyPlayer.m_pAPlayer == NULL)
return -1;
std::string strFileName = filename;
std::wstring wsFileName = StringConvert::ANSIToUnicode(strFileName);
m_MyPlayer.SetFileName(wsFileName);
if (bPlayAudio)
m_MyPlayer.m_pAPlayer->SetConfig(15, _T("1"));
else
m_MyPlayer.m_pAPlayer->SetConfig(15, _T("0"));
if(hwnd == NULL)
{
hwnd = (long)GetSafeHwnd();
}
if(hwnd)
{
m_MyPlayer.m_hAPlayerWnd = (HWND)hwnd;
if (::IsWindow(m_MyPlayer.m_hAPlayerWnd))
{
HRESULT hr = AtlAxAttachControl(m_MyPlayer.m_pAPlayer, m_MyPlayer.m_hAPlayerWnd, NULL);
if(FAILED(hr))
return -1;
}
}
/**/
BOOL bResult = m_MyPlayer.OpenFile((wchar_t *)wsFileName.c_str());
if(!bResult)
return -1;
double dbDuration = 0.0;
HANDLE hEventOpen = m_MyPlayer.GetEventOpen();
BOOL fQuit = SyncWaitEventAndMessage(hEventOpen);
if(fQuit)
dbDuration = m_MyPlayer.GetDuration()/1000.0; // unit: second.
else
dbDuration = -1;
return dbDuration;
}
startPlayback就是这样的一个函数。
可是,当我将上述代码放置到一个ocx控件中的时候,OnOpenSucceeded得不到执行。请问,为什么??