问题描述
您好,我开发了一个程序来讲网页的内容.
这是我使用的代码:
Hello I have developed a program to speak the contents of a web page.
Here is the code i do this with:
public void SpeakSynthText(String TextToSpeak, bool SpeechRecognitionEnable)
{
#region SpeechSynth
if (SpeechRecognitionEnable == true)
{
//starts the speech engine
this.Start();
}
// Initialize a new instance of the SpeechSynthesizer.
SpeechSynthesizer synth = new SpeechSynthesizer();
if (Boolean.Parse(SpeechSaveEnabled) == true)
{
//FolderDialogSaveSpeechToDirectory();
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "wave files (*.wav)|*.wav|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
TB.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
WaveFileLoc = saveFileDialog1.FileName;
synth.SetOutputToWaveFile(WaveFileLoc);
synth.Rate = -1;
}
}
// Select a voice that matches a specific gender.
synth.SelectVoiceByHints(VoiceGender.Female);
// Speak a text string synchronously and play back the output file.
synth.Speak(TextToSpeak);
synth.Dispose();
//synth.SetOutputToWaveFile();
verbalcommandmode = "true";
if (Boolean.Parse(SpeechSaveEnabled) == true)
{
System.Media.SoundPlayer m_SoundPlayer =
new System.Media.SoundPlayer(@WaveFileLoc);
m_SoundPlayer.Play();
m_SoundPlayer.Dispose();
}
#endregion
}
当我想说网页的内容时,我传递以下参数:
when i want to speak the contents of a web page i pass the arguments of:
this.SpeakSynthText(WIKI.Document.Body.InnerText, false);
现在,当我运行此命令时,UI会冻结直到它说完为止,所以我被告知必须在与UI线程不同的线程上运行该函数.
所以我阅读了我在codeproject上可以找到的所有文章,这就是我得到的:
now when i run this the UI freezes up until its done speaking, so i was told that i would have to run the function on a thread separate of the UI thread.
So i read all the articles i could find on codeproject and this is what i got:
private void SpeakPageText_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(RunInThread));
t.Start();
//WIKI.Document.Body.InnerText
}
当我单击与此方法关联的按钮时,会调用此函数(对不起,我一直称呼它们为我在python中编码的函数)
当我运行此方法时,应该在新线程中运行RunInThread方法. RunInThread方法如下所示:
this function(sorry i keep calling them functions i am used to coding in python) is called when i click the button associated with this method
when i run this method it is SUPPOSED to run the RunInThread method in a new thread. The RunInThread method goes like this:
public void RunInThread()
{
//Option 1
try
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(delegate { this.SpeakSynthText(WIKI.Document.Body.InnerText, false); }));
}
}
catch (InvalidOperationException oex)
{
MessageBox.Show(oex.Message);
}
}
我感觉它不是在单独的线程上运行RunInThread,而是在主线程上运行,那么如何在新线程上运行此方法?
还是无法解决问题,如何使用语音合成器说出文字却没有冻结UI?
非常感谢您的所有帮助.
i have a feeling that it isn''t running RunInThread on a separate thread but instead on the main thread, so how do run this method on a new thread?
or if that won''t work how can i speak text with the speech synthesizer but not have the UI freeze up?
All of your help is very much appreciated.
推荐答案
synth.SpeakAsync(...);
几个链接:
http://msdn.microsoft.com/zh-我们/library/system.speech.synthesis%28v=vs.100%29.aspx [ ^ ]
http://msdn.microsoft.com/zh-cn/library/system.speech.synthesis.speechsynthesizer%28v = vs.100%29.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/ms586891%28v = vs.100%29.aspx [ ^ ]
您几乎没有活动要订阅:
http://msdn.microsoft. com/en-us/library/system.speech.synthesis.speechsynthesizer.speakstarted%28v = vs.100%29.aspx [ ^ ]
http://msdn.microsoft. com/en-us/library/system.speech.synthesis.speechsynthesizer.speakprogress%28v = vs.100%29.aspx [ ^ ]
http://msdn.microsoft. com/en-us/library/system.speech.synthesis.speechsynthesizer.speakcompleted%28v = vs.100%29.aspx [ ^ ]
[更新]
两年前,我写了一篇有关MS Text-To-Speech的文章.文章使用波兰语,但源代码使用英语.您可以在此处下载源代码:
http://kozub.net.pl/download/sources/SayIT%20source.zip [ ^ ]
二进制文件在这里:
http://kozub.net.pl/download/programs/SayIT.zip [ ^ ]
博客文章位于此处:
http://kozub.net.pl/2012/04 /01/sayit-czyli-jak-sprawic-aby-komputer-przemowil/ [ ^ ]
示例项目允许您使用默认的Windows声音输出播放TextBox中的所有或选定的文本.您可以从系统上可用的语音中选择语音,获取有关所选语音的信息并控制播放速度和音量.它也可以将输出另存为WAV文件.您可以配置WAV文件选项,例如每个样本的位数,通道和质量.它还显示有关播放进度,位置等的信息.
希望对您有用:)
Few links:
http://msdn.microsoft.com/en-us/library/system.speech.synthesis%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/ms586891%28v=vs.100%29.aspx[^]
And you have few events to subscribe:
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.speakstarted%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.speakprogress%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.speakcompleted%28v=vs.100%29.aspx[^]
[Update]
Two years ago I wrote post about MS Text-To-Speech. Article is in polish language but source code is in english. You can download source code here:
http://kozub.net.pl/download/sources/SayIT%20source.zip[^]
Binary files here:
http://kozub.net.pl/download/programs/SayIT.zip[^]
Blog post is located here:
http://kozub.net.pl/2012/04/01/sayit-czyli-jak-sprawic-aby-komputer-przemowil/[^]
Sample project allows you to play all or selected text from TextBox using default windows sound output. You can select voice from voices available on your system, get info about selected voice and control playback speed rate and volume. It can save output as WAV files as well. You can configure WAV file options such as bits per sample, channels and quality. Also it displays informations about playback progress, position etc.
I hope you find this useful :)
这篇关于C#在新线程中运行文本到语音合成器以保持UI响应能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!