[英]How to convert Text to Wave using SAPI with multithreading?
我正在嘗試使用以下函數將文本轉換為wave文件。 如果從主UI線程調用它,它工作正常。 但是從另一個線程調用時失敗了。 如何從多線程函數中調用它?
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();
}
你有沒有Coinitialized其他線程? 需要使用它在每個線程上初始化COM。 另外..你使用在另一個線程中的一個線程中創建的COM對象嗎? 因為你需要編寫線程之間的接口,如果你這樣做...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.