gpt4 book ai didi

c++ - SAPI(微软语音 API)CoCreateInstance 失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:23:48 24 4
gpt4 key购买 nike

我正在尝试运行来自 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;
}

最佳答案

您必须使用 CoInitialize[Ex] 初始化线程在使用 CoCreateInstance API 之前。您得到的错误代码应该明确表明:CO_E_NOTINITIALIZED(您应该在您的问题上发布它!)。

关于c++ - SAPI(微软语音 API)CoCreateInstance 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22231176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com