SAPI:应用程序未发言

问题描述 投票:1回答:3

请看下面的代码

main.cpp

    #define _ATL_APARTMENT_THREADED

#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override something,
//but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>

#include <sapi.h>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    cout << "Hello" << endl;
    ISpVoice * pVoice = NULL;

    if (FAILED(::CoInitialize(NULL)))
        return FALSE;

    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    if( SUCCEEDED( hr ) )
    {
        cout << "Succeeded" << endl;
        hr = pVoice->Speak(L"Hello world", 0, NULL);
        pVoice->Release();
        pVoice = NULL;
    }
    else
    {
        cout << "Not succeeded" << endl;
    }

    ::CoUninitialize();
    return TRUE;
}

当我运行此代码时,窗口将打开,并显示“ Hello”消息。但没有声音发出!它应该说“ Hello World”!为什么会这样?

以下是QT .pro设置,如果需要的话

以下是QT .pro设置

#-------------------------------------------------
#
# Project created by QtCreator 2013-05-03T14:31:00
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = Speech
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

INCLUDEPATH += "C:/Program Files/Microsoft Speech SDK 5.1/Bin"
INCLUDEPATH += "C:/Program Files/Microsoft Speech SDK 5.1/Include"



LIBS += "C:/Program Files/Microsoft Speech SDK 5.1/Lib/i386/sapi.lib"
LIBS += "C:/Program Files/Microsoft SDKs/Windows/v7.0A/Lib/User32.lib"
c++ windows qt text-to-speech sapi
3个回答
0
投票

您在语音有时间结束之前要在pVoice对象上调用Release。该API具有waitUntilDone函数,在释放对象之前可能需要该函数。


0
投票

调用pVoice->Speak(L"Hello world", 0, NULL)时,尝试使用SPF_ASYNC而不是0。这是我的方法:

[...]   

HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, reinterpret_cast<void**>(&pVoice));
if( SUCCEEDED( hr ) )
{        

    const wchar_t* reqAttributs = L"Language=409"; // 409 = en_US; 809 = en_UK; 40C = fr_FR
    const wchar_t* optAttributs = L"Gender=Female"; // or L"Gender=Male"

    ISpObjectToken* cpTokenEng;
    if (FAILED(::SpFindBestToken(SPCAT_VOICES, reqAttributs, optAttributs, &cpTokenEng))) {
        throw std::exception("Couldn't find a Token with the required attributs.");
    }

    pVoice->SetVoice(cpTokenEng);

    hr = pVoice->Speak(L"Hello World", SPF_ASYNC, nullptr);
        if (hr == S_OK) {
            // OK

        } else if (hr == E_INVALIDARG) {
            // One or more parameters are invalid

        } else if (hr == E_POINTER) {
            // Invalid pointer

        } else if (hr == E_OUTOFMEMORY) {
            // Exceeded available memory

        } else {
            // Unknown error
        }

        hr = pVoice->WaitUntilDone(INFINITE);

        pVoice->Release();
        pVoice = nullptr;
}

[...]

令牌部分实际上不是必需的,但是如果您要自定义声音(为了获得最佳效果,必须在计算机上安装多个声音),它可能会很有用。


0
投票

此问题是驱动程序错误或应用程序冲突。我的笔记本电脑是Dell inspiron 4030,这是发生此问题的笔记本电脑。在我的台式机上工作。

© www.soinside.com 2019 - 2024. All rights reserved.