Introduction
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.
Step 1. Add the NameSpace as Using SpeechLib.
Step 2. Create an object of SpVoice class.
SpVoice voice = new SpVoice();
Step 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);
Example
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.
using System;
using System.Windows.Forms;
using SpeechLib; // Add this using directive for SpeechLib namespace
namespace VoiceTest {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void btnSpeak_Click(object sender, EventArgs e) { // Corrected EventArgs
if (!string.IsNullOrEmpty(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