2014-03-06 68 views
1

我试图从a Microsoft sample page运行SAPI示例。SAPI(Microsoft Speech API)CoCreateInstance失败

当运行应用程序(具有VS2010),此行失败:

hr = cpVoice.CoCreateInstance(CLSID_SpVoice); 

小时返回错误代码,并且不执行其他所有代码。

我不知道为什么我错了,因为我认为正确使用该页面中的示例代码,并且我从未使用过此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; 
} 
+0

你告诉使用HRESULT是一个错误代码,但你没有说明代码是什么。 –

+0

@AdrianMcCarthy你是对的。我的歉意(我迟到了,我回到了我的女朋友生日派对的家中......)。下次我会更加小心。 – Jepessen

回答

1

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

+0

感谢您的回复。它解决了我的问题。抱歉缺少错误代码,下次我会更加小心。再次感谢。 – Jepessen

相关问题