Text to speech using C#



This Article explains how to convert text to speech using C#.

Before starting to write code for converting a text into speech you have to add a reference (DownLoad if not available) for Interop.SpeechLib.

Speed1.gif

  1. Add the NameSpace as Using SpeechLib.
  2. Create an object of SpVoice class

    SpVoice voice = new SpVoice();
     
  3. Call the Speak() method by using Spvoice class object as follows and pass the parameters in the Speak()

    voice.Speak(String text,SpeechVoiceSpeakFlags Flags);

Let's Look the code:

            SpVoice voice = new SpVoice();

            voice.Volume = 100;

            voice.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);

Place the above code in the Button Click event.

Add one TextBox, write some text in TextBox and click on the button to convert the TextBox text into speech as follows:

Speed2.gif

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using SpeechLib;

namespace VoiceTest
{

public partial class Form1 : Form
{

public Form1()
{

InitializeComponent();

}

private void btnSpeak_Click(object sender,EventArgse)
{

if (textBox1.Text != "")
{
SpVoice voice = new SpVoice();
voice.Volume = 100;
voice.Speak(textBox1.Text,SpeechVoiceSpeakFlags.SVSFlagsAsync);

}

else
{
MessageBox.Show("Please enter text for speech", "Text to Speech", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

}

}

Thanks for Reading my article!

Syed Shakeer Hussain
 

Up Next
    Ebook Download
    View all
    Learn
    View all