我正在尝试从Microsoft示例页中运行一个SAPI示例。
当我运行应用程序(使用VS2010)时,这一行失败:
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;
}发布于 2014-03-06 17:49:16
在使用CoInitialize[Ex] API之前,必须使用CoCreateInstance初始化线程。您得到的错误代码应该明确地建议:CO_E_NOTINITIALIZED (您应该将它发布在您的问题上!)。
https://stackoverflow.com/questions/22231176
复制相似问题