我有100个纽扣在一个酒杯里。每个按钮都执行类似的操作,即表示自己的数目。假设Button60将演讲60,button100将演讲100。
我用了这些密码:
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
...............
private void Form1_Load(object sender, EventArgs e)
{
seme_comboBox.SelectedIndex = 0;
dpt_comboBox.SelectedIndex = 0;
foreach (var button in Controls.OfType<Button>())
{
button.Click += button_Click;
}
}然后
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
string text = button.Name.Substring("button".Length);
synthesizer.Speak(text);
}但是,如果我连续地点击两个按钮,那么切换另一个按钮和讲话至少需要2到3秒。而且它的声音也不够响亮。所以我需要提高按钮在一个小的duration.And内的动作的性能,我也想增加演讲的声音。我该怎么做?
发布于 2013-09-16 14:50:34
这听起来像是SpeechSynthesizer阻塞了UI线程。
您可以使用SpeakAsync()代替(从http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.speakasync.aspx)进行以下操作
注意,您可能希望或不希望取消所有(注释)的行:
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
string text = button.Name.Substring("button".Length);
synthesizer.SpeakAsyncCancelAll(); // cancel anything that's playing
synthesizer.SpeakAsync(text);
}否则,您可能会在另一个线程中运行sythesizer。
您可以使用.Volume属性控制声音音量:
synthesizer.Volume = 100; // maximum volume (range 0-100)https://stackoverflow.com/questions/18830367
复制相似问题