Reputation: 286
I'm currently developing an UWP DLL application using the namespace Windows::Media::SpeechSynthesis
. I created a function which allows me to speak an entered text which looks as follow:
int TextToSpeechUwpDll::ttsSpeak( const char* text )
{
SpeechSynthesizer ^speak = ref new SpeechSynthesizer();
MediaPlayer ^player = ref new MediaPlayer;
int wchars_num = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
wchar_t* texts = new wchar_t[wchars_num];
MultiByteToWideChar( CP_ACP, 0, text, -1, texts, wchars_num );
String ^sentence = ref new String( texts );
task<SpeechSynthesisStream ^> speakTask = create_task( speak->SynthesizeTextToStreamAsync( sentence ) );
speakTask.then( [player, sentence]( SpeechSynthesisStream ^speechStream )
{
player->Source = MediaSource::CreateFromStream( speechStream, speechStream->ContentType );
player->AutoPlay = true;
player->Play();
Sleep( 3000 );
} );
return true;
}
However, the Sleep( 3000 );
needs to be removed because when I enter a sentence, it is only spoken for 3 seconds meaning that if it is longer than that, the rest won't be spoken.
I tried to remove this line, but when I do it, nothing is spoken at all. No sound comes out. The extension of the duration doesn't either solve my problem.
What I'm looking for is the ability to speak a sentence without a time limit. Does someone had this issue before?
Upvotes: 0
Views: 115
Reputation: 5868
You should declare a private property about your MediaPlayer ^player to hold it in case local variable "player" is released.
in .h
class TextToSpeechUwpDll
{
//......
private:
MediaPlayer ^player = ref new MediaPlayer();
};
in .cpp
SpeechSynthesizer ^speak = ref new SpeechSynthesizer();
char* text = "helloworld";
int wchars_num = MultiByteToWideChar(CP_ACP, 0, text, -1, NULL, 0);
wchar_t* texts = new wchar_t[wchars_num];
MultiByteToWideChar(CP_ACP, 0, text, -1, texts, wchars_num);
String ^sentence = ref new String(texts);
task<SpeechSynthesisStream ^> speakTask = create_task(speak->SynthesizeTextToStreamAsync(sentence));
speakTask.then([this, sentence](SpeechSynthesisStream ^speechStream)
{
player->Source = MediaSource::CreateFromStream(speechStream, speechStream->ContentType);
player->AutoPlay = true;
player->Play();
});
Upvotes: 0