In the previous blog, I talked about some knowledge points of Microsoft Speech Platform. This blog uses an example to illustrate how to use it.
The example is to read a piece of text. when the text is read, the text turns red. That's simple.
First look at the form layout.
650) this. width = 650; "title =" aa.png "src =" http://www.bkjia.com/uploads/allimg/131228/1606344338-0.png "/>
Implementation Code:
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; using Microsoft. speech. synthesis; using System. threading; namespace SpeechPro {public partial class PlayForm: Form {public PlayForm () {InitializeComponent ();} /// <summary> /// TTS object /// </summary> Speech Synthesizer speech; /// <summary> /// separate the text content into an array /// </summary> /// <param name = "content"> text content </param> // /<returns> character array </returns> string [] GetArr (string content) {char [] charArr = new char [] {'. ','; '}; // Delimiter string [] arr = content. split (charArr, StringSplitOptions. removeEmptyEntries); // separate int sumcount = 0; // variable of the total number of characters for (int I = 0; I <arr. length; I ++) {sumcount + = arr [I]. length; // The total number of accumulated variables. arr [I] + = content. substring (sumcount, 1); // get the separator sumcount + = 1; // length of the punctuation} return arr;} string [] arr; /// <summary> /// playback content /// </summary> void Play () {arr = GetArr (Play_TB.Text); // design the TTS object, and set the object speech = new SpeechSynthesizer (); speech. setoutputtodefaauaudiodevice (); speech. volume = 100; speech. rate = 0; speech. speakStarted + = speech_SpeakStarted; // asynchronous speech reading for (int I = 0; I <arr. length; I ++) {PromptBuilder pb = new PromptBuilder (); pb. appendText (arr [I], PromptRate. medium); Prompt p = new Prompt (pb); speech. speakAsync (p) ;}} int index = 0; // read the index value int sum = 0; // read the total length /// <summary> /// read the current event from Prompt /// </summary> /// <param name = "sender"> </param >/// <param name = "e"> </param> void speech_SpeakStarted (object sender, speakStartedEventArgs e) {if (index> 0) {// restore the previous record to the original style Play_TB.Select (sum-arr [index-1]. length, arr [index-1]. length); Play_TB.SelectionColor = Color. black;} // set the style Play_TB.Select (sum, arr [index]. length); Play_TB.SelectionColor = Color. red; // Add length and read index sum + = arr [index]. length; index ++; this. text = "reading the sentence" + index + ";} /// <summary> /// read button /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void Play_But_Click (object sender, eventArgs e) {Play ();}}}
This article is from "Gui Su Wei" blog, please be sure to keep this source http://axzxs.blog.51cto.com/730810/1247881