Question

I have 100 buttons in a winform. Each button performs similar action which is to speech the number of its own. Say Button60 will speech 60, button100 will speech 100.

I used these codes:

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;
        }


    }

then

    private void button_Click(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        string text = button.Name.Substring("button".Length);

        synthesizer.Speak(text);
    }

But if i click two buttons sequentially then it takes at least 2 or 3 seconds to switch another button and to speech. And also its sound is not enough loud. So i need to to increase the performance of the button's action within a small duration.And also i want to increase the sounds of the speech. How can i do this???

Was it helpful?

Solution

It sounds like the SpeechSynthesizer is blocking the UI thread.

You could try the following, using SpeakAsync() instead (from http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.speakasync.aspx)

Note you may or may not want the line that cancels all (commented):

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);
}

Failing that you could probably run the sythesizer in another thread.

You can control the sound volume with the .Volume property:

synthesizer.Volume = 100; // maximum volume (range 0-100)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top