C++ MSAPI 5: SetNotifyCallbackFunction not working



所以我尝试了带有事件示例的MSAPI 5.4 TTS。现在我创建了一个cmd提示应用程序,它使用SetNotifyCallbackFunction,但我传递的函数没有被调用。我不是C++专家,所以我很难解决这个问题,有人能给我指明正确的方向吗,或者至少给我一个SetNotifyCallbackFunction的好例子吗?

这是我的代码的简化版本:

typedef void __stdcall SPNOTIFYCALLBACK(WPARAM wParam, LPARAM lParam);
void __stdcall outsideeventFunction(WPARAM, LPARAM);
void __stdcall outsideeventFunction(WPARAM wParam, LPARAM lParam){
    std::cout << "Event called::wParam: " << wParam << " lParam: " << lParam << std::endl;
    SPEVENT eventItem;
    memset(&eventItem, 0, sizeof(SPEVENT));
    while (SUCCEEDED(pV->GetEvents(1, &eventItem, NULL)))
    {
        bool exitNa = false;
        switch (eventItem.eEventId)
        {
        case SPEI_WORD_BOUNDARY:
            SPVOICESTATUS eventStatus;
            pV->GetStatus(&eventStatus, NULL);
            ULONG start, end;
            start = eventStatus.ulInputWordPos;
            end = eventStatus.ulInputWordLen;
            std::cout << "From event Test: " << start << ", " << end << std::endl;
            std::cout << "From event Length: " << theString.length() - 1 << ", " << start + end << std::endl;
            if (theString.length() - 1 <= start + end){
                std::cout << "From event Exit!" << std::endl;
                exitNa = true;
            }
            break;
        }
        SpClearEvent(&eventItem);
        if (exitNa){
            break;
        }
    }
    return;
}
int _tmain(int argc, TCHAR* argv [], TCHAR* envp [])
{
    pV = NULL;
    std::string nativeString("Hello world, this is a test! For the purpose of a longer message, I'll add another sentence. And here comes the new sentence.");
    SPNOTIFYCALLBACK *cb = &outsideeventFunction;
    if (FAILED(::CoInitialize(NULL)))
        return FALSE;
    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **) &pV);
    if (SUCCEEDED(hr))
    {
        if (SUCCEEDED(pV->SetNotifyCallbackFunction(cb, 0, 0))){
            std::cout << "Success adding callback" << std::endl;
        }
        ULONGLONG ullMyEvents = SPFEI(SPEI_WORD_BOUNDARY);
        pV->SetInterest(ullMyEvents, ullMyEvents);
    }
    theString = std::wstring(nativeString.begin(), nativeString.end());
    printf("Speak: %sn", nativeString.c_str());
    hr = pV->Speak(theString.c_str(), SPF_ASYNC, NULL);
    pV->WaitUntilDone(INFINITE);
    std::system("pause");
    pV->Release();
    pV = NULL;
    ::CoUninitialize();
    return TRUE;
}

这个应用程序的结果是,单词的合成很顺利,但outsideventFunction从未被调用。如您所见,SetInterest设置正确。我该怎么解决这个问题?

两个问题:

1) SAPI不允许您重新分配事件目标,因此行

pV->SetNotifyWin32Event();

总会失败。

2) 您需要发送消息来传递事件。因此,与其调用

pV->WaitUntilDone(INFINITE);

在设置事件之前,您需要获取句柄和泵消息:

HANDLE hWait = pV->SpeakCompleteEvent();
WaitAndPumpMessagesWithTimeout(hWait, INFINITE);
HRESULT WaitAndPumpMessagesWithTimeout(HANDLE hWaitHandle, DWORD dwMilliseconds)
{
    HRESULT hr = S_OK;
    BOOL fContinue = TRUE;
    while (fContinue)
    {
        DWORD dwWaitId = ::MsgWaitForMultipleObjectsEx(1, &hWaitHandle, dwMilliseconds, QS_ALLINPUT, MWMO_INPUTAVAILABLE);
        switch (dwWaitId)
        {
        case WAIT_OBJECT_0:
            {
                fContinue = FALSE;
            }
            break;
        case WAIT_OBJECT_0 + 1:
            {
                MSG Msg;
                while (::PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
                {
                    ::TranslateMessage(&Msg);
                    ::DispatchMessage(&Msg);
                }
            }
            break;
        case WAIT_TIMEOUT:
            {
                hr = S_FALSE;
                fContinue = FALSE;
            }
            break;
        default:// Unexpected error
            {
                fContinue = FALSE;
                hr = E_FAIL;
            }
            break;
        }
    }
    return hr;
}

相关内容

  • 没有找到相关文章

最新更新