Hello Programmers,
Here you are gonna learn how to convert Text to Speech in C# i.e you are going to create a software that Talks!
So lets start;
Steps
1. Open Visual Studio -> New Project -> Windows Form Application with name
TexttoSpeech.
2. Now add button, richtextbox and labels to the empty form and design it as shown in
the below screenshot.
3. Now change the name and text of the added controls from the Properties dialog box;
Control Control Name Text
Button btnRead Read
Richtextbox rtbTexttoRead
4. Now double click on the Read button and copy the below code;
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SetOutputToDefaultAudioDevice();synth.Speak(rtbTexttoRead.Text);
5. Now you will have to add a reference to System.Speech.Synthesis to
use SpeechSynthesizer in your project. To do so, Right click on your Project from
Solution Explorer and select Add Reference ( See the Screenshot)
Now check the System.Speech as shown in the below screenshot
6. Now Right click on SpeechSynthesizer() and Resolve -> Using System.Speech.Synthesis;
(See the Screenshot)
7. The complete code is shown below;
Form1.cs
using System;
using System.Speech.Synthesis;
using System.Windows.Forms;
namespace TexttoSpeech
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnReadText_Click(object sender, EventArgs e)
{
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SetOutputToDefaultAudioDevice();
synth.Speak(rtbTexttoRead.Text);
}
}
}
Screenshot
8. Rebuild and Run the project, type some text in the richtextbox and click on Read to
read the text ENJOY :-)
Conclusion
In case of any doubts / errors, please drop in a comment.
See Also
No comments:
Post a Comment