2009-10-05 6 views
3

J'essaie de convertir le texte en fichier wave en utilisant la fonction suivante. Cela fonctionne très bien s'il est appelé depuis le thread principal de l'interface utilisateur. Mais il échoue lors de l'appel d'un autre thread. Comment l'appeler à partir d'une fonction multi-thread?Comment convertir du texte en Wave en utilisant SAPI avec multithreading?

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

définir échoue. Ce qui échoue Quel message d'erreur, le cas échéant, obtenez-vous? – Glen

Répondre

2

Avez-vous CoInitialized l'autre thread? COM doit être initialisé sur chaque thread en l'utilisant. Aussi .. utilisez-vous un objet COM créé dans un thread dans un autre thread? Parce que vous devez marshall l'interface entre threads si vous faites cela ...

+0

Un grand merci pour votre réponse. J'ai oublié d'appeler CoInitialize() et CoUninitialize() qui a causé le problème. Maintenant, il fonctionne après avoir ajouté les appels d'initialisation appropriés. Pouvez-vous s'il vous plaît me dire pourquoi la même fonction a fonctionné lors de l'appel à partir du thread de l'interface utilisateur principale même sans appel à CoInitialize()? – Vadakkumpadath

+0

Probablement parce que quelque chose d'ELSE a appelé CoInitialize sur le thread principal. Initialisation MFC peut-être? – Goz

Questions connexes