This tutorial will demonstrate how to use the Microsoft Speach Object Library within an ASP.NET 4.0 web site using C#.

Adding the Default.aspx Page

To demonstrate using text to speach with ASP.NET we will need to create a simple web site in which we will allow users to type in text and then use the Microsoft Speech Object Library to speak it. At this point I have created a new ASP.NET Empty Web Site. To begin:

  1. Right click the project in your solution explorer.
  2. Select add new item…
  3. Select a web form.
  4. Name it ‘Default.aspx’.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a textbox onto the web form.
  8. Change the ID property to ‘txtSpeak’.
  9. Change the TextMode property to ‘MultiLine’.
  10. Drag and drop a button underneath the textbox.
  11. Change the ID property to ‘btnSpeak’.
  12. Change the Text property to ‘Speak’.

Teaching it to Speak

Next, we want to add in a reference to the Microsoft Speech Object Library and add in some code so that it will speak the text in the textbox. To do this:

  1. Right click the project in your solution explorer.
  2. Select Add Reference..
  3. Click the COM tab.
  4. Select Microsoft Speech Object Libary.
  5. Click ok.

Now that we have the proper references we can add the code to btnSpeak in the event that the user clicks it. To do this:

  1. Open Default.aspx up to design mode.
  2. Double click btnSpeak to generate the btnSpeak_Click event method.
  3. At the top of the Default.aspx.cs class add the following using statement:
  4. Add the following code to the btnSpeak_Click event method:

Testing

To test this out, go ahead and load up the website. Ensure that you have some medium to hear the speech and that your sound is enabled. Type some text into the textbox and click speak, you should hear the text being spoken.
Download Source Files