Background
Day by day business needs change depending on business requirements and fulfilling these requirements BPO and KPO come that require a physical assistance of humans to support specific requirements but some requirements exist in the application to convert text into speech without a human involved. So by considering the preceding requirement I decided to write this article.
To convert text to speech the SpeechSynthesizer class is used. So let us learn step-by-step how to convert text to speech.
To convert text to speech the SpeechSynthesizer class is used. So let us learn step-by-step how to convert text to speech.
The SpeechSynthesizer Class
SpeechSynthesizer is a sealed class containing various methods, events and properties that help to convert text to speech with various options. The SpeechSynthesizer class is in the System.Speech.Synthesis namespace that contains classes that allow initialization and configuration of a speech synthesis engine, create prompts, generate speech, respond to events and modify voice characteristics. Speech synthesis is often referred to as Text-To-Speech or TTS.
The following are some of the common properties of the SpeechSynthesizer class.
Properties of SpeechSynthesizer class
- Rate: Used to specify speech rate.
- State: Used to check the current state of the SpeechSynthesizer class, whether it is paused, resume or a speaking state.
- Voice: Gets the current voice information.
- Volume: Sets the volume.
Events of SpeechSynthesizer class
- BookmarkReached.
- PhonemeReached.
- SpeakCompleted.
- SpeakProgress.
- SpeakStarted.
- StateChanged.
- VisemeReached.
- VoiceChange
Methods of SpeechSynthesizer class
- AddLexicon
- Dispose
- GetCurrentlySpokenPrompt
- GetInstalledVoices
- GetInstalledVoices
- Pause
- RemoveLexicon
- Resume
- SelectVoice
- SelectVoiceByHints
- SetOutputToDefaultAudioDevice
- SetOutputToNull
- SetOutputToWaveFile
- SetOutputToWaveStream
- Speak
- SpeakAsync
- SpeakAsyncCancel
- SpeakAsyncCancelAll
- SpeakSsml
- SpeakSsmlAsync
Now let us see the preceding explanation by creating a sample web application as follows:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty WebSite" (to avoid adding a master page).
- Provide the web site a name such as "TextToSppech" or another as you wish and specify the location.
- Then right-click on the Solution Explorer and select "Add New Item" and Add Web Form.
- Drag and drop one Button, a TextBox onto the <form> section of the Default.aspx page.
Now the default.aspx page source code will looks as follows.
- <%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ConvertTextToSpeech.Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Article by Vithal Wadje</title>
- </head>
- <body bgcolor="navy">
- <form id="form2" runat="server">
- <div style="color: White;">
- <h4>
- Article for C#Corner
- </h4>
- <br />
- <table width="100%">
- <tr>
- <td>
- <asp:TextBox ID="txtMsg" TextMode="MultiLine" runat="server" Height="142px" Width="380px"></asp:TextBox><br />
- </td>
- </tr>
- </table>
- <br />
- <table>
- <tr>
- <td>
- <asp:Button ID="btVoice" runat="server"
- Text="convert to speech" onclick="btnVoice_Click" />
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
After adding the reference the Solution Explorer will look as follows:
Now double-click on the convert to speech button and write the following code:
- protected void btnVoice_Click(object sender, EventArgs e)
- {
- // creating the object of SpeechSynthesizer class
- SpeechSynthesizer sp = new SpeechSynthesizer();
- //setting volume
- sp.Volume = 100;
- //ing text box text to SpeakAsync method
- sp.SpeakAsync(txtMsg.Text);
- }
- using System;
- using System.Speech.Synthesis;
- namespace ConvertTextToSpeech
- {
- public partial class Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnVoice_Click(object sender, EventArgs e)
- {
- // creating the object of SpeechSynthesizer class
- SpeechSynthesizer sp = new SpeechSynthesizer();
- //setting volume
- sp.Volume = 100;
- //ing text box text to SpeakAsync method
- sp.SpeakAsync(txtMsg.Text);
- }
- }
- }
Now run the application. The UI will look such as follows:
Now enter some text in the preceding text box as in the following:
Now click on the Convert to speech button. It will convert text to speech.
Notes
- Enter proper readable text.
- Ensure your system sounds are working.
- For more details and explanation, download the Uploaded Zip file.
Summary
From all the preceding examples you have learned how to convert text to speech. I hope this article is useful for all readers, if you have a suggestion then please contact me.

sathish sirikondaPosted Aug 21, 2023, 11:11 AM
I using the same technique on asp.net on a webform but after playing the speech, the webform falls into endless loading loop. Do you have any suggestions for a solution for me? I Tried to load the speech in another webform and redirect back to the index.aspx after playing the speech but it dont works , it also falls into endless loading loop
venkat tejaPosted Dec 6, 2020, 1:55 AM
This is not working in live server, but it is working on local server. Pls help me.
venkat tejaPosted Dec 6, 2020, 1:54 AM
Dear Vithal
Rumeli BanerjeePosted Apr 3, 2019, 2:58 PM
Why it loading after each time pressing the button
dhiraj dhirajPosted Mar 28, 2019, 2:40 AM
When client requests then server is getting busy and other requests also not performed by server
dhiraj dhirajPosted Mar 28, 2019, 2:39 AM
On server side its not working
Kuldeep jhaPosted Oct 7, 2017, 4:27 AM
Its not working on server
Narendran NPosted Aug 3, 2016, 8:12 AM
Hi, I have migrated an existing web app for Visually Impaired persons, so for it's working fine in English language both the Speech Synthesizer & Speech Recognition, but my problem is I need to do the same stuffs for Tamil and Hindi languages(already loaded the texts from db), the Speech Synthesizer should read my hindi/ tamil texts. Please do the needful.
Narendran NPosted May 11, 2016, 2:34 AM
Am getting an error: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.
Narendran NPosted May 11, 2016, 2:28 AM
Helpful article,how to read the texts from grid view row?
Shakti Singh DulawatPosted Apr 21, 2016, 5:18 AM
Nice article and very well explain
Ashish KachhiPosted Nov 22, 2015, 4:58 AM
page is continously loading after button press,plz give solution to stop page loading. Thnx....
Vithal WadjePosted Aug 19, 2015, 2:25 PM
david chen sir ,check the references of dll on server
Vithal WadjePosted Aug 19, 2015, 2:24 PM
thanks sachith wickramaarachchi sir
david chenPosted Aug 18, 2015, 5:52 PM
is it working when the web app is published to the server? it did not work for me, but localhost is OK
sachith wickramaarachchiPosted Mar 12, 2015, 4:33 AM
Very useful :D
Vithal WadjePosted Mar 10, 2015, 10:20 AM
venkata krishnaiah sir set page directive Async="true". for more detail refer above code
Vithal WadjePosted Mar 10, 2015, 10:19 AM
Thanks Nitin Tyagi sir
NitinPosted Mar 10, 2015, 3:18 AM
fantastic one.
krish pothuriPosted Mar 9, 2015, 1:29 PM
i have to try, but i get error is Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.
krish pothuriPosted Mar 9, 2015, 1:28 PM
i will try but i get error is ,
Vithal WadjePosted Mar 9, 2015, 10:31 AM
thanks Sourabh Somani sir
Vithal WadjePosted Mar 9, 2015, 10:31 AM
thanks Khargesh Rajput sir
Sourabh SomaniPosted Mar 9, 2015, 4:10 AM
Nice One
Khargesh RajputPosted Mar 9, 2015, 2:24 AM
thanks for sharing sir
Vithal WadjePosted Mar 8, 2015, 12:03 PM
Thanks Rahul Saxena sir
Vithal WadjePosted Mar 8, 2015, 12:02 PM
Thanks Manish Kumar Choudhary sir
Rahul Kumar SaxenaPosted Mar 8, 2015, 8:56 AM
Good show Vithal Wadjee....
Manish Kumar ChoudharyPosted Mar 8, 2015, 8:06 AM
Really great one. Thanks for sharing Vithal Wadje sir.
Vithal WadjePosted Mar 8, 2015, 7:12 AM
thanks Tom Mohan sir
Tom MohanPosted Mar 8, 2015, 6:49 AM
Helpful
Vithal WadjePosted Mar 8, 2015, 4:21 AM
Thanks ROHAN PANDEY sir
ROHAN PANDEYPosted Mar 8, 2015, 4:16 AM
Very usefull .
Vithal WadjePosted Mar 8, 2015, 3:54 AM
Thanks Anil Kumar sir
Vithal WadjePosted Mar 8, 2015, 3:54 AM
Thanks Harpreet Singh sir
Anil KumarPosted Mar 8, 2015, 3:27 AM
great work.. well in detail..
Harpreet SinghPosted Mar 8, 2015, 1:15 AM
I was looking for something like this...really helpful.Thanks for sharing it sir :)