Reputation: 377
I would like to have a combo box on my form which allows the user to select the voice which they would like to use. How can I implement such a feature?
Currently, my form consists of four buttons and a combo box. The code behind the buttons and the synthesizer is as follows:
private void button1_Click(object sender, EventArgs e)
{
reader.Dispose();
if (Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text != "")
{
reader = new SpeechSynthesizer();
reader.SpeakAsync(Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text)
button2.Enabled = true;
button4.Enabled = true;
reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(reader_SpeakCompleted);
}
else
{
MessageBox.Show("Please insert text before launching Text to Speech.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
void reader_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Speaking)
{
reader.Pause();
button3.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Paused)
{
reader.Resume();
}
button3.Enabled = false;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (reader != null)
{
reader.Dispose();
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
}
I would like to populate a combo box with a list of currently installed voices, which, when the user clicks one, reads the text from richTextBoxPrintCtrl1 in the selected voice. Currently, the synthesizer works, but I would like to add this feature to my Text to Speech feature.
Thanks.
Upvotes: 1
Views: 1335
Reputation: 4057
This code should do roughly what you are after (if you are still interested:). You will need to drag on a new comboxbox called 'comboBox1' onto your form
private SpeechSynthesizer reader = new SpeechSynthesizer();
private void PopulateInstalledVoices()
{
foreach (InstalledVoice voice in
reader.GetInstalledVoices(new CultureInfo("en-US")))
{
VoiceInfo info = voice.VoiceInfo;
comboBox1.Items.Add(info.Name);
}
}
private void Form1_Load(object sender, EventArgs e)
{
PopulateInstalledVoices();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var voice = comboBox1.SelectedItem as String;
if (voice != null)
{
reader.SelectVoice(voice);
}
}
private void button1_Click(object sender, EventArgs e)
{
reader.SpeakAsync("this is a test message");
}
Upvotes: 3