我目前正在使用命名空间Windows::Media::SpeechSynthesis
开发UWP DLL应用程序。我创建了一个函数,使我可以说出输入的文本,如下所示:
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;
}
但是,Sleep( 3000 );
需要被删除,因为当我输入一个句子时,它只会说3秒钟,这意味着如果它长于该句子,则其余的将不会被说出来。
我试图删除此行,但是当我这样做时,什么也没说。没有声音发出。持续时间的延长并不能解决我的问题。
我要寻找的是能够不受时间限制地讲句子的能力。以前有人遇到过这个问题吗?
答案 0 :(得分:0)
您应该为您的 MediaPlayer ^ player 声明一个私有属性,以在释放局部变量“ player”的情况下保留它。
在.h
class TextToSpeechUwpDll
{
//......
private:
MediaPlayer ^player = ref new MediaPlayer();
};
在.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();
});