Examples and memos of using TTS technology on webpages

Source: Internet
Author: User
Tags sapi

TTS is a text-to-speech engine launched by Microsoft. We can use it to read the text on the webpage and give us a break.

A demo has been provided in samples/scripts/simpletts of Microsoft Speech SDK 5.1, but is in full English. A problem is encountered in reference to the modification, that is

 

The instance variable definition code of the COM Object SAPI. spvoice must be separated from the initialization and event processing code.

 

That is to say, the definition code of the SAPI. spvoice instance variable voiceobj:

 

// Create the SAPI spvoice object <br/> var voiceobj = new activexobject ("SAPI. spvoice ");

 

Put it in a <SCRIPT> </SCRIPT> or in a. js file, such as speak0.js.

 

Code for initialization and event handling of the SAPI. spvoice instance variable voiceobj:

 

Function initializecontrols () <br/>{< br/> // initialize the voices and audiooutput select boxes <br/> var voicestoken = voiceobj. getvoices (); <br/> var audiooutputen en = voiceobj. getaudiooutputs (); <br/>}</P> <p> initializecontrols (); </P> <p> function voiceobj: endstream () <br/>{< br/> idbspeaktext. value = "read aloud"; <br/>}</P> <p> function voiceobj: startstream () <br/>{< br/> idbspeaktext. value = "stop"; <br/>}</P> <p>

Put it in another <SCRIPT> </SCRIPT> or another. js file, such as speak1.js.

 

DEMO code:

 

 

<HTML> <br/> <pead> <br/> <script language = "JavaScript"> <br/> // create the SAPI spvoice object <br/> var voiceobj = new activexobject ("SAPI. spvoice "); <br/> </SCRIPT> </P> <p> <script language = "JavaScript"> </P> <p> // code in the body of the webpage is used to initialize controls and <br/> // to handle SAPI events </P> <p>/***** initializer code *****/</P> <p> function initializecontrols () <br/>{< br/> // INITIA Lize the voices and audiooutput select boxes <br/> var voicestoken = voiceobj. getvoices (); <br/> var audiooutputen en = voiceobj. getaudiooutputs (); <br/>}</P> <p> initializecontrols (); </P> <p> // handle endstream event <br/> function voiceobj :: endstream () <br/>{< br/> idbspeaktext. value = "read aloud"; <br/>}</P> <p> // handle startstream event <br/> function voiceobj: startstream () <br/>{< br/> idbspeaktext. value = "Stop"; <br/>}</P> <p> // speaktext () function: <br/> // This function gets the text from the textbox and sends it to the <br/> // voice object's speak () function. the value "1" for the second <br/> // parameter corresponds to the svsflagsasync value in the speechvoicespeakflags <br/> // enumerated type. <br/> function speaktext () <br/> {<br/> // alert (document. body. innertext); <br/> If (idbspeaktext. value =" Read aloud ") <br/>{< br/> // speak the string in the edit box <br/> try <br/>{< br/> voiceobj. speak (document. body. innertext, 1); <br/>}< br/> catch (exception) <br/>{< br/> alert ("error "); <br/>}< br/> else if (idbspeaktext. value = "stop") <br/>{< br/> // speak empty string to stop current speaking. the value "2" for <br/> // The second parameter corresponds to the svsfpurgebeforespeak <br/> // value in the S Peechvoicespeakflags enumerated type. <br/> voiceobj. speak ("", 2 ); <br/>}< br/> </SCRIPT> </P> <p> <script for = "window" event = "onquit () "Language =" JavaScript "> <br/> // clean up voice object <br/> Delete voiceobj; <br/> </SCRIPT> <br/> </pead> </P> <p> <body> <br/> <button id = "idbspeaktext" onclick =" speaktext (); "> read aloud </button> <br/> <p> the German team was killed in Spain as I expected. </P> <br/> <p> the German team swept across England and Argentina and immediately attracted a large number of tops. </P> <br/> <p> In fact, anyone familiar with European club events knows that the German club team's performance in Spain is not satisfactory. Even in the Spanish League, the average performance of hetafi can embarrass the Bundesliga leader Bayern Munich. Barcelona, the leader in La Liga, has been making frequent record to the German team over the past few years. It not only has a dominant score against Bremen, Stuttgart, Schalke 04, and Bayern Munich, in addition, the technical advantages in ball control and pass passing are also very obvious. </P> <br/> <p> this time, the number of first players in the Spanish team against Germany reached 7 in Barcelona. All the players except baldes were playing, pujor and pique are on the back line. Harvey, buskz, and inesta play in the midfield, while Juan and Pedro play the forward. In addition, fablregas and Rena are also from the Barcelona youth training camp. The German team's first lineup includes players from Bayern, including kluze, schwensge and ram. If it is not Thomas Muller's cumulative yellow card suspension, it will certainly be one of the first players, podorsky and trochwski also played for Bayern. In addition, badthtubel, Tony Cros, Gomes, and Bret on the bench are also from Bayern, that is to say, this man-riman chariot also has 10 "Bayern gangs" players. In a certain sense, this competition is Barcelona vs Bayern Munich, and the Spanish team is very competitive. </P> <br/> </body> <br/> </ptml>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.