Description:-
In this Example we will create Test to speech in windows form for that we need Speech Synthesizer in windows form for speaking or voice. So let’s start first design and after we will code for speaking.
Code:-
For that we need following name spaces in our windows form.
In this Example we will create Test to speech in windows form for that we need Speech Synthesizer in windows form for speaking or voice. So let’s start first design and after we will code for speaking.
Code:-
SpeechSynthesizer speaker = new SpeechSynthesizer(); bool flag = false; public Form1() { InitializeComponent(); speaker.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs> (speaker_SpeakCompleted); btnPause.Enabled = false; foreach (InstalledVoice voice in speaker.GetInstalledVoices()) { cbVoice.Items.Add(voice.VoiceInfo.Name); } } void speaker_SpeakCompleted(object sender, SpeakCompletedEventArgs e) { btnPlay.Enabled = true; btnPause.Enabled = false; } private void Play(string words) { speaker.SelectVoice(cbVoice.Text); speaker.SpeakAsync(words); } private void btnPlay_Click(object sender, EventArgs e) { if (cbVoice.SelectedIndex >= 0) { btnPlay.Enabled = false; btnPause.Enabled = true; Play(txtWords.Text); } else { MessageBox.Show("Please select a voice", "Text to Speech", MessageBoxButtons.OK, MessageBoxIcon.Warning); cbVoice.Focus(); } } private void btnPause_Click(object sender, EventArgs e) { if (speaker != null) { if (flag == false) { speaker.Pause(); btnPause.Text = "Resume"; flag = true; } else { speaker.Resume(); btnPause.Text = "Pause"; flag = false; } } } private void btnDownload_Click(object sender, EventArgs e) { FolderBrowserDialog browser = new FolderBrowserDialog(); if (browser.ShowDialog() == DialogResult.OK) { SpeechSynthesizer MySynthesizer = new SpeechSynthesizer(); MySynthesizer.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs> (MySynthesizer_SpeakCompleted); MySynthesizer.SetOutputToWaveFile(string.Concat(browser.SelectedPath, "\\MyTTS.wav")); PromptBuilder builder = new PromptBuilder(); builder.AppendText(txtWords.Text); MySynthesizer.SpeakAsync(builder); } } void MySynthesizer_SpeakCompleted(object sender, SpeakCompletedEventArgs e) { MessageBox.Show("Audio downloaded sucessfully", "Text to Speech", MessageBoxButtons.OK, MessageBoxIcon.Information); }
For that we need following name spaces in our windows form.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Speech.Synthesis;
Now run your windows
form and add text what you want to hear in voice in windows form.
Thanks for comments.....