【发布时间】:2014-04-09 11:36:45
【问题描述】:
我正在尝试从 a Microsoft sample page 运行 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;
}
【问题讨论】:
-
你告诉 use HRESULT 是一个错误代码,但你没有说代码是什么。
-
@AdrianMcCarthy 你说得对。我很抱歉(我迟到了,我要回家参加我女朋友的生日聚会......)。下次我会更小心的。
标签: c++ text-to-speech sapi