C++ SAPI(Microsoft语音API)CoCreateInstance失败
我正试着从中运行SAPI样本 当我运行应用程序(使用VS2010)时,此行失败:C++ SAPI(Microsoft语音API)CoCreateInstance失败,c++,text-to-speech,sapi,C++,Text To Speech,Sapi,我正试着从中运行SAPI样本 当我运行应用程序(使用VS2010)时,此行失败: hr = cpVoice.CoCreateInstance( CLSID_SpVoice ); hr返回一个错误代码,所有其他代码不执行 我不知道为什么我错了,因为我想正确使用该页面中的示例代码,而且我以前从未使用过这个API 这是我完整的main.cpp文件。我错过了什么 #include "stdafx.h" #include <sapi.h> #include <sphelper.h>
hr = cpVoice.CoCreateInstance( CLSID_SpVoice );
hr返回一个错误代码,所有其他代码不执行
我不知道为什么我错了,因为我想正确使用该页面中的示例代码,而且我以前从未使用过这个API
这是我完整的main.cpp文件。我错过了什么
#include "stdafx.h"
#include <sapi.h>
#include <sphelper.h>
#include <atlcomcli.h>
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = S_OK;
CComPtr <ISpVoice> cpVoice;
CComPtr <ISpStream> cpStream;
CSpStreamFormat cAudioFmt;
//Create a SAPI Voice
hr = cpVoice.CoCreateInstance( CLSID_SpVoice );
//Set the audio format
if(SUCCEEDED(hr))
{
hr = cAudioFmt.AssignFormat(SPSF_22kHz16BitMono);
}
//Call SPBindToFile, a SAPI helper method, to bind the audio stream to the file
if(SUCCEEDED(hr))
{
hr = SPBindToFile( L"c:\\ttstemp.wav", SPFM_CREATE_ALWAYS,
&cpStream, & cAudioFmt.FormatId(),cAudioFmt.WaveFormatExPtr() );
}
//set the output to cpStream so that the output audio data will be stored in cpStream
if(SUCCEEDED(hr))
{
hr = cpVoice->SetOutput( cpStream, TRUE );
}
//Speak the text "hello world" synchronously
if(SUCCEEDED(hr))
{
hr = cpVoice->Speak( L"Hello World", SPF_DEFAULT, NULL );
}
//close the stream
if(SUCCEEDED(hr))
{
hr = cpStream->Close();
}
//Release the stream and voice object
cpStream.Release ();
cpVoice.Release();
return 0;
}
#包括“stdafx.h”
#包括
#包括
#包括
int _tmain(int argc,_TCHAR*argv[]
{
HRESULT hr=S_正常;
首席执行官;
CComPtr cpStream;
尾状体;
//创建SAPI语音
hr=cpVoice.CoCreateInstance(CLSID_SpVoice);
//设置音频格式
如果(成功(hr))
{
hr=尾叶叶叶型(SPSF_22kHz16BitMono);
}
//调用SPBindToFile(一种SAPI助手方法)将音频流绑定到该文件
如果(成功(hr))
{
hr=SPBindToFile(L“c:\\ttstemp.wav”,SPFM\u CREATE\u ALWAYS,
&cpStream,&cAudioFmt.FormatId(),cAudioFmt.WaveFormatExPtr();
}
//将输出设置为cpStream,以便将输出音频数据存储在cpStream中
如果(成功(hr))
{
hr=cpVoice->SetOutput(cpStream,TRUE);
}
//同步讲课文“hello world”
如果(成功(hr))
{
hr=cpVoice->Speak(L“Hello World”,SPF_默认为空);
}
//关闭小溪
如果(成功(hr))
{
hr=cpStream->Close();
}
//释放流和语音对象
cpStream.Release();
cpVoice.Release();
返回0;
}
在使用CoCreateInstance
API之前,必须先使用初始化线程。您得到的错误代码应该明确指出:CO_E_NOTINITIALIZED
(您应该在您的问题上发布它!)。您告诉use HRESULT是一个错误代码,但您没有说明代码是什么。@AdrianMcCarthy您完全正确。我的道歉(我迟到了,我要回家参加我女朋友的生日派对…)。下次我会更小心的。谢谢你的回复。它解决了我的问题。很抱歉丢失了错误代码,下次我会更加小心的。再次感谢。