gpt4 book ai didi

c++ - SAPI:应用程序不说话

转载 作者:太空宇宙 更新时间:2023-11-04 14:07:23 27 4
gpt4 key购买 nike

请看下面的代码

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"

最佳答案

尝试在调用 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;
}

[...]

Token 部分不是必需的,但如果您想自定义语音(为了获得最佳效果,您的计算机上必须安装多个语音),它会很有用。

关于c++ - SAPI:应用程序不说话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16361781/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com