Implement Text to Speach in ASP.NET 4.0 and C#
Adding the Default.aspx PageTo 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:
- Right click the project in your solution explorer.
- Select add new item…
- Select a web form.
- Name it ‘Default.aspx’.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a textbox onto the web form.
- Change the ID property to ‘txtSpeak’.
- Change the TextMode property to ‘MultiLine’.
- Drag and drop a button underneath the textbox.
- Change the ID property to ‘btnSpeak’.
- Change the Text property to ‘Speak’.
We used over 10 web hosting companies before we found Server Intellect. Our new cloud server was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.
Teaching it to SpeakNext, 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:
- Right click the project in your solution explorer.
- Select Add Reference..
- Click the COM tab.
- Select Microsoft Speech Object Libary.
- 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:
- Open Default.aspx up to design mode.
- Double click btnSpeak to generate the btnSpeak_Click event method.
- At the top of the Default.aspx.cs class add the following using statement:Code BlockDefault.aspx.csThe using statement we need to create the SpVoice object.
using SpeechLib;
- Add the following code to the btnSpeak_Click event method:Code BlockDefault.aspx.csThe btnSpeak_Click event method.
protected void btnSpeak_Click(object sender, EventArgs e) { //create voice object SpVoice Voice = new SpVoice(); //have it speak the text in out textbox Voice.Speak(txtSpeak.Text); }
If you’re looking for a really good web host, try Server Intellect – we found the setup procedure and their control panel, very easy to adapt to and their IT team is awesome!
TestingTo 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.
text 2 speach asp4 csharp
text 2 speach asp4 csharp
No comments:
Post a Comment