C-Sharpcorner - Latest Articles: 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

Step 1: Create New Project -> Select Windows Form Application in C# -> Give it a name.

Step 2: Drop RichTextBox and four Button Controls from the ToolBox.

Step 3: Right click on Reference Folder -> Click on Add Reference.

Step 4: Select System.Speech Assembly and Click on OK.

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

Step 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.)

  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.   
  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.   
  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.   
  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.   
  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

Hope you like it. Thanks.

分享给朋友:
您可能感兴趣的文章:
随机阅读: