Text to Speech Converter in C#

In this article, we will learn how to create a Text to Speech Converter in a C# Windows Forms application using the SpeechSynthesizer class.

Let's Begin

Step 1

Create a new project then select Windows Forms application in C# and give it a name.



Step 2

Drop a RichTextBox and four Button Controls from the ToolBox.



Step 3

Right-click on the References Folder then click on Add Reference.



Step 4

Select the System.Speech Assembly and click on OK.



After adding it, you will see System.Speech assembly under the References folder.



Step 5

Go to Form1.cs (code behind .cs file) and add System.Speech (the System.Speech namespace contains types that support speech recognition) and System.Speech.Synthesis namespace (the System.Speech.Synthesis namespace contains classes for initializing and configuring a speech synthesis engine and for generating speech and so on).

  1. using System;   
  2. using System.Windows.Forms;   
  3. using System.Speech;   
  4. using System.Speech.Synthesis;   
  5.   
  6. namespace TextToSpeechConverter   
  7. {   
  8.    public partial class Form1 : Form   
  9.    {   
  10.       public Form1()   
  11.       {   
  12.          InitializeComponent();   
  13.       }   
  14.       //SpeechSynthesizer Class Provides access to the functionality of an installed a speech synthesis engine.   
  15.       SpeechSynthesizer speechSynthesizerObj;   
  16.       private void Form1_Load(object sender, EventArgs e)   
  17.       {   
  18.          speechSynthesizerObj = new SpeechSynthesizer();   
  19.          btn_Resume.Enabled = false;   
  20.          btn_Pause.Enabled = false;   
  21.          btn_Stop.Enabled = false;   
  22.       }   
  23.   
  24.   
  25.       private void btn_Speak_Click(object sender, EventArgs e)   
  26.       {   
  27.          //Disposes the SpeechSynthesizer object   
  28.          speechSynthesizerObj.Dispose();   
  29.          if(richTextBox1.Text!="")   
  30.          {   
  31.             speechSynthesizerObj = new SpeechSynthesizer();   
  32.             //Asynchronously speaks the contents present in RichTextBox1   
  33.             speechSynthesizerObj.SpeakAsync(richTextBox1.Text);   
  34.             btn_Pause.Enabled = true;   
  35.             btn_Stop.Enabled = true;   
  36.          }   
  37.       }   
  38.   
  39.       private void btn_Pause_Click(object sender, EventArgs e)   
  40.       {   
  41.          if(speechSynthesizerObj!=null)   
  42.          {   
  43.             //Gets the current speaking state of the SpeechSynthesizer object.   
  44.             if(speechSynthesizerObj.State==SynthesizerState.Speaking)   
  45.             {   
  46.                //Pauses the SpeechSynthesizer object.   
  47.                speechSynthesizerObj.Pause();   
  48.                btn_Resume.Enabled = true;   
  49.                btn_Speak.Enabled = false;   
  50.             }   
  51.          }   
  52.       }   
  53.   
  54.       private void btn_Resume_Click(object sender, EventArgs e)   
  55.       {   
  56.          if (speechSynthesizerObj != null)   
  57.          {   
  58.             if (speechSynthesizerObj.State == SynthesizerState.Paused)   
  59.             {   
  60.                //Resumes the SpeechSynthesizer object after it has been paused.   
  61.                speechSynthesizerObj.Resume();   
  62.                btn_Resume.Enabled = false;   
  63.                btn_Speak.Enabled = true;   
  64.             }   
  65.          }   
  66.       }   
  67.   
  68.       private void btn_Stop_Click(object sender, EventArgs e)   
  69.       {   
  70.          if(speechSynthesizerObj!=null)   
  71.          {   
  72.             //Disposes the SpeechSynthesizer object   
  73.             speechSynthesizerObj.Dispose();   
  74.             btn_Speak.Enabled = true;   
  75.             btn_Resume.Enabled = false;   
  76.             btn_Pause.Enabled = false;   
  77.             btn_Stop.Enabled = false;   
  78.          }   
  79.       }   
  80.    }   
  81. }   
Final Preview



I hope you like this. Thanks.


Similar Articles