請看看下面的代碼SAPI:應用程序沒有講話
的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 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()'返回的'hr'的值。更重要的是,你怎麼知道'CoCreateInstance'成功了? – john 2013-05-03 14:54:05
@john:我不知道,我是這個主題的新手。這個例子可以在MS網站上找到。 – 2013-05-03 14:57:45
那麼如果Merlin的答案沒有幫助,那麼你需要像'if(SUCCEEDED(hr)){...} else {cout <<「CoCreateInitialise失敗!」<< hex << hr <<「\ n」; }'和你從'pVoice-> Speak()'得到的hr值相同。始終檢查錯誤代碼。 – john 2013-05-03 15:00:29