【问题标题】:SAPI (Microsoft Speech API) CoCreateInstance failedSAPI (Microsoft Speech API) CoCreateInstance 失败
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 感谢您的回复。它解决了我的问题。抱歉遗漏了错误代码,下次我会更加小心。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2012-04-30
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多