文本转语音 TTS(MS Speech SDK)问题??
如下一段代码(主要是 MyPlayText() 函数),我要的功能是将我的 一段字符串 转换成 语音流。
这段代码在 控制台程序运行时,一切OK 。但是 ,我把这个功能(即 MyPlayText() 函数)放到 Windows服务程序 里面 ,pVoice->Speak 处总是失败 ,GetLastError 返回的是 1008(试图引用不存在的令牌)。
小弟水平有限 ,不知该怎么弄了... 请大家帮帮忙 !!
PS:sdk是“SpeechSDK51.exe”,环境XP+VC6
代码:
- C/C++ code
#include <windows.h> #include <sapi.h> #include <wininet.h> #include <assert.h> #include <cstdio> #include <sphelper.h> #include <WCHAR.h>#pragma comment(lib, "wininet") void MyPlayText();int main(int argc, char* argv[]) { MyPlayText(); return TRUE; } void MyPlayText() { ISpeechVoice* pVoice = NULL; ISpeechBaseStream* stream=NULL; ISpeechAudioFormat* pFormat=NULL; ISpeechObjectTokens* ObjectTokens=NULL; ISpeechObjectToken* pToken=NULL; VARIANT vt; VARIANT vt1; long StreamNumber; if (FAILED(::CoInitialize(NULL))) { OutputDebugString("==> ::CoInitialize(NULL) failed"); return; } HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpeechVoice, (void**)&pVoice); if (SUCCEEDED(hr)) { HRESULT hr = CoCreateInstance(CLSID_SpMemoryStream, NULL, CLSCTX_ALL, IID_ISpeechBaseStream, (void**)&stream); if (SUCCEEDED(hr)) { try { OutputDebugString("-----> ----->"); stream->get_Format(&pFormat); pFormat->put_Type(SAFT8kHz16BitMono); pVoice->putref_AudioOutputStream(stream); pVoice->put_AllowAudioOutputFormatChangesOnNextSet(FALSE); pVoice->GetVoices(L"", L"", &ObjectTokens); ObjectTokens->Item(0, &pToken); pVoice->putref_Voice(pToken); StreamNumber = 1000; HRESULT hr = pVoice->Speak(L"TTTTTTaaaaaaaa", SVSFDefault, &StreamNumber); if (SUCCEEDED(hr)) {OutputDebugString("pVoice->Speak OK");} else {OutputDebugString("pVoice->Speak not OK");} memset(&vt, 0, sizeof(vt)); memset(&vt1, 0, sizeof(vt1)); hr = stream->Seek(vt, SSSPTRelativeToEnd, &vt1); if (SUCCEEDED(hr)) {OutputDebugString("stream->Seek OK");} else {OutputDebugString("stream->Seek not OK");} OutputDebugString("<----- <-----"); } catch(...) { OutputDebugString("-----> some err"); } } pVoice-> Release(); pVoice = NULL; } ::CoUninitialize(); }[解决办法]
做成服务啊,对服务不怎么熟习,不知道他和普通的应用程序有什么区别
看你的情况 好像是应为执行环境导致的错误
试试 换成
ServiceStatus.dwServiceType =
SERVICE_WIN32_OWN_PROCESS
[解决办法]
TTS对应的驱动什么的,装了么?我记得应该要装驱动的!
[解决办法]
一般安装包,都会带一个demo的,你运行demo,看看能否实现tts,如果demo可以,那么,再检查自己的程序不迟!
[解决办法]
遇到这样的问题,换个编译器先,用个更新的编译器试试
[解决办法]
看你这么坚持不懈。。
你每一步都有做返回值检查吧
HRESULT hr = pVoice->Speak(L"TTTTTTaaaaaaaa", SVSFDefault, &StreamNumber);
然后到这里的时候 检查返回值返回错误把 那么hr的值是多少
为什么一定要做成服务呢