2014-03-06 7 views
1

a Microsoft sample page에서 SAPI 샘플을 실행하려고합니다. 나는 (VS2010)와 응용 프로그램을 실행할 때SAPI (Microsoft Speech API) CoCreateInstance failed

,이 라인은 실패

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

당신은 이전에 CoCreateInstance API를 사용하여 CoInitialize[Ex]를 사용하여 스레드를 초기화해야합니다. 오류 코드는 명시 적으로 다음과 같이 제안해야합니다 : CO_E_NOTINITIALIZED (질문에 게시해야합니다!).

+0

답장을 보내 주셔서 감사합니다. 그것은 내 문제를 해결했다. 미안 해요 오류 코드, 다음에 더 조심하게 될거야. 다시 한번 감사드립니다. – Jepessen

관련 문제