Text to Speech Converter in C#


Text to Speech Converter in C#

In this Article, We will learn How to create Text to Speech Converter in C# Windows Form Application using SpeechSynthesizer Class.

Let's Begin:
1) Create New Project -> Select Windows Form Application in C# -> Give it a name.
Add New Project Image
2) Drop RichTextBox and four Button Controls from the ToolBox.
Text to Speech Converter Image2
3) Right click on Reference Folder -> Click on Add Reference.
Add referece Image3
4) Select System.Speech Assembly and Click on OK.
Add Reference System.Speech Image4
After adding it, you will see System.Speech assembly under References folder.
System.Speech in Reference Image5
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;
            }
        }
       
    }
}
Final Preview:
Text to Speech Converter in C# Windows Form Application
Hope you like it. Thanks.

Share this

Related Posts

Latest
Previous
Next Post »