Text to Speech Converter in C#
Let's Begin:
1) Create New Project -> Select Windows Form Application in C# -> Give it a name.
2) Drop RichTextBox and four Button Controls from the ToolBox.
3) Right click on Reference Folder -> Click on Add Reference.
4) Select System.Speech Assembly and Click on OK.
After adding it, you will see System.Speech assembly under References folder.
5) Go to Form1.cs(Code behind .cs file) and Add System.Speech(System.Speech namespaces contain types that support speech recognition) and System.Speech.Synthesis namespace (System.Speech.Synthesis namespace contains classes for initializing and configuring a speech synthesis engine and for generating speech etc.)
using System;
using
System.Windows.Forms;
using System.Speech;
using
System.Speech.Synthesis;
namespace
TextToSpeechConverter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//SpeechSynthesizer Class Provides access to the
functionality of an installed a speech synthesis engine.
SpeechSynthesizer
speechSynthesizerObj;
private void Form1_Load(object sender, EventArgs e)
{
speechSynthesizerObj
= new SpeechSynthesizer();
btn_Resume.Enabled = false;
btn_Pause.Enabled = false;
btn_Stop.Enabled = false;
}
private void btn_Speak_Click(object sender, EventArgs e)
{
//Disposes the SpeechSynthesizer object
speechSynthesizerObj.Dispose();
if(richTextBox1.Text!="")
{
speechSynthesizerObj = new SpeechSynthesizer();
//Asynchronously speaks the contents present in
RichTextBox1
speechSynthesizerObj.SpeakAsync(richTextBox1.Text);
btn_Pause.Enabled = true;
btn_Stop.Enabled = true;
}
}
private void btn_Pause_Click(object sender, EventArgs e)
{
if(speechSynthesizerObj!=null)
{
//Gets the current speaking state of the
SpeechSynthesizer object.
if(speechSynthesizerObj.State==SynthesizerState.Speaking)
{
//Pauses the SpeechSynthesizer object.
speechSynthesizerObj.Pause();
btn_Resume.Enabled = true;
btn_Speak.Enabled = false;
}
}
}
private void btn_Resume_Click(object sender, EventArgs e)
{
if (speechSynthesizerObj != null)
{
if
(speechSynthesizerObj.State == SynthesizerState.Paused)
{
//Resumes the SpeechSynthesizer object
after it has been paused.
speechSynthesizerObj.Resume();
btn_Resume.Enabled = false;
btn_Speak.Enabled = true;
}
}
}
private void btn_Stop_Click(object sender, EventArgs e)
{
if(speechSynthesizerObj!=null)
{
//Disposes the SpeechSynthesizer object
speechSynthesizerObj.Dispose();
btn_Speak.Enabled
= true;
btn_Resume.Enabled = false;
btn_Pause.Enabled = false;
btn_Stop.Enabled = false;
}
}
}
}
|
Hope you like it. Thanks.