2009-10-05 4 views
3

다음 기능을 사용하여 텍스트를 웨이브 파일로 변환하려고합니다. 기본 UI 스레드에서 호출하면 정상적으로 작동합니다. 그러나 다른 스레드에서 호출 할 때 실패합니다. 멀티 스레드 함수에서 호출하는 방법?멀티 쓰레딩과 SAPI를 사용하여 텍스트를 Wave로 변환하는 방법?

void Pan_Channel::TextToPlaybackFile(CString Text, CString FileName) 
{ 
// Result variable 
HRESULT Result = S_OK; 

// Voice Object 
CComPtr<ISpVoice> cpVoice; 

// Create a SAPI Voice 
Result = cpVoice.CoCreateInstance(CLSID_SpVoice); 

// Audio format 
CSpStreamFormat cAudioFmt; 

// Set the audio format 
if(SUCCEEDED(Result)) 
{ 
    Result = cAudioFmt.AssignFormat(SPSF_8kHz16BitMono); 
} 

// File Stream 
CComPtr<ISpStream> cpStream; 

// Call SPBindToFile, a SAPI helper method, to bind the audio stream to the file 
if(SUCCEEDED(Result)) 
{ 
    Result = SPBindToFile(FileName, 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(Result)) 
{ 
    Result = cpVoice->SetOutput(cpStream, TRUE); 
} 

    // Speak the text syncronously 
if(SUCCEEDED(Result)) 
{ 
    Result = cpVoice->Speak(Text.AllocSysString(), SPF_DEFAULT, NULL); 
} 

// close the stream 
if(SUCCEEDED(Result)) 
{ 
    Result = cpStream->Close(); 
} 

// Release stream 
cpStream.Release(); 

// Release voice object 
cpVoice.Release(); 
} 
+0

정의가 실패합니다. 실패한 것. 무엇이 있다면, 오류 메시지가 나타 납니까? – Glen

답변

2

다른 스레드와 Coinitialized 했습니까? COM은이를 사용하는 각 스레드에서 초기화해야합니다. 또한 .. 다른 스레드에서 한 스레드에서 만든 COM 개체를 사용합니까? 그렇게하면 스레드 간의 인터페이스를 마샬링해야하기 때문에 ...

+0

답장을 보내 주셔서 감사합니다. 문제를 일으킨 CoInitialize() 및 CoUninitialize()를 호출하는 것을 잊어 버렸습니다. 이제는 적절한 초기화 호출을 추가 한 후 작동 중입니다. CoInitialize()를 호출하지 않고도 기본 UI 스레드에서 호출했을 때 같은 함수가 왜 작동하는지 알려주실 수 있습니까? – Vadakkumpadath

+0

아마도 ELSE가 주 스레드에서 CoInitialize를 호출했기 때문일 수 있습니다. MFC의 초기화는 어쩌면? – Goz