这是我的代码:
#include <sapi.h>
int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice;);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(L"Hello world", 0, NULL);
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
这是我的第一个SAPI 5.1 Run,这是MS提供的示例hello世界。
我已将SAPI“ bin”位置添加到系统变量“ Path”。
以下是我的QT项目的内容(.pro内容)
#-------------------------------------------------
#
# Project created by QtCreator 2013-04-26T12:59:05
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = Tired
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
ContentDetector.cpp \
Speech.cpp
INCLUDEPATH += C:/opencv/build/include
INCLUDEPATH += C:/opencv/build/include/opencv
INCLUDEPATH += "C:/Program Files/Java/jdk1.7.0/include"
INCLUDEPATH += "C:/Program Files/Java/jdk1.7.0/include/win32"
INCLUDEPATH += "C:/Program Files/Microsoft Speech SDK 5.1/Include"
LIBS += C:/opencv/build/x86/vc9/lib/opencv_calib3d240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_contrib240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_core240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_features2d240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_flann240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_highgui240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_imgproc240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_objdetect240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_video240.lib
LIBS += "C:/Program Files/Java/jdk1.7.0/lib/jvm.lib"
LIBS += "C:/Program Files/Java/jdk1.7.0/lib/jawt.lib"
LIBS += "C:/Program Files/Microsoft Speech SDK 5.1/Lib/i386/sapi.lib"
请注意,SAPI包含在最后一个include中,SAPI库是最后一个库。
当我运行此代码时,将产生跟随错误。
C:\Users\yohan\Documents\QTPeojects\Tired\Speech.cpp:10: error: C2143: syntax error : missing ')' before ';'
C:\Users\yohan\Documents\QTPeojects\Tired\Speech.cpp:10: error: C2059: syntax error : ')'
我不明白为什么。
按照约翰的指示,以上错误现在消失了。但是,现在生成了以下错误。为什么会这样?
Speech.obj:-1: error: LNK2019: unresolved external symbol __imp__CoUninitialize@0 referenced in function _main
Speech.obj:-1: error: LNK2019: unresolved external symbol __imp__CoCreateInstance@20 referenced in function _main
Speech.obj:-1: error: LNK2019: unresolved external symbol __imp__CoInitialize@4 referenced in function _main
release\Tired.exe:-1: error: LNK1120: 3 unresolved externals
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice;);
应该是
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
您的版本中还有一个分号。
缺少的功能在user32.lib中(但不知道如何将其添加到您的qt proj中)
您的链接器问题源于您没有在Qt .pro文件中正确引用库的事实。
要添加库,您需要执行以下操作:-
LIBS + = -LC:/ opencv / build / x86 / vc9 / lib / -lopencv_calib3d240
注意库路径前的-L和库名称前的-l。
唯一的方法是添加ATL。
#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;
}