System. Speech. Synthesis adds the pause and resume functions.

Source: Internet
Author: User

To facilitate the call of the pause and continue methods. Write the speech function into a class. Directly attach the Code:

1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. speech. synthesis; 5 using system. text; 6 using system. speech; 7 8 namespace windowsformsapplication1 9 {10 public class texttospeak 11 {12 // to implement pause and other functions, call the speechsynthesizer object 13 speechsynthesizer speech = new speechsynthesizer (); 14 15 /// <summary> 16 /// read 17 /// </Summary> 18 /// <Param name = "text"> text </param> 19/ // <Param name = "rate"> speed </param> 20 public void speak (string text, int rate) 21 {22 speech. rate = rate; 23 speech. speak (text); 24 speech. dispose (); 25} 26 27 /// <summary> 28 // read 29 /// </Summary> 30 /// <Param name = "text"> text </param> 31 // <Param name = "rate"> language speed </param> 32 // <Param name = "gender"> gender </param> 33 public void speak (string text, int rate, string gender) 34 {35 36 speech. rate = rate; 37 Try 38 {39 if (gender. tolower () = "male") 40 {41 speech. selectvoice ("VW Paul"); 42} 43 else if (gender. tolower () = "female") 44 {45 speech. selectvoice ("VW Kate"); 46} 47 48} 49 // The exception is that 50 catch (argumentexception) 51 {52 53 throw is not installed in the voice database; 54} 55 finally 56 {57 speech. speak (text); 58 speech. dispose (); 59} 60} 61 62 // <summary> 63 // Save the audio file 64 // </Summary> 65 // <Param name = "path"> path </param> 66 // <Param name = "text"> text </param> 67 // <Param name = "rate"> language speed </param> 68/ // <Param name = "gender"> gender, can be null </param> 69 public void savewavefile (string path, string text, int rate, string gender) 70 {71 speech. rate = rate; 72 try 73 {74 if (gender. tolower () = "male") 75 {76 speech. selectvoice ("VW Paul"); 77} 78 else if (gender. tolower () = "female") 79 {80 speech. selectvoice ("VW Kate"); 81} 82 83} 84 catch (argumentexception) 85 {86 87 throw; 88} 89 finally 90 {91 speech. setoutputtowavefile (PATH); 92 Speech. speak (text); 93 speech. setoutputtonull (); 94} 95} 96 97 Public void speakpause () 98 {99 speech. pause (); 100} 101 102 public void speakresume () 103 {104 speech. resume (); 105} 106 107 Public void speakstop () 108 {109 speech. dispose (); 110} 111} 112}

I use winfrom to implement this class. I didn't want to add a click event to the button at first, call the Speak METHOD OF THE texttospeak class, and add the speakpause function to another button. I copied several paragraphs of English during the runtime, and found that the form was suspended, but there was still sound output. This was a little messy. Think about this situation, it should be similar to multithreading. Don't laugh at me. winfrom hasn't done much, and Asp.net has been around for a long time. I searched the implementation method of the progress bar on the Internet, and finally decided to use backgroundworker. It is more convenient to look at what others wrote ~~~

Let's briefly introduce backgroundworker. It lies in the toolbar. It has several methods.

1. This is the backgroundworker=dowork (objectsender, doworkeventargs e) process running in the background ). Here we will put the Speak method of the exttospeak class. It is called using backgroundworker1.runwokerasync (parameter, not added. Therefore, put this method in the Click Event of the playback button.

2. backgroundworkerprogress progresschanged: This is a method that can be executed at runtime. Use backgroundworker1.reportprogress (INT.

3. backgroundworkerappsrunworkercompleted: This is executed at the end of the process.

Specific please see: http://www.cnblogs.com/inforasc/archive/2009/10/12/1582110.html

I directly put a RichTextBox in winfrom to write the English to be read. At first, I took it for granted that the value of RichTextBox. Text was directly transferred in the speak in backgroundworkerincludowork (objectsender, doworkeventargs E), but there was no response at runtime. I didn't know what went wrong at first. I tried it. The background code is not easy to adjust, and you cannot directly debug the code. You can only use a prompt statement to help. The result is stuck in RichTextBox. text. It cannot read this value. What's the reason ~~~ But why can I read the language speed plug-in value, because it is an int type .. So what should we do? We can only pass parameters when calling the click event. I have two parameters. One is text and the other is gender. That's the string array.

Code:

1  _ttSpeak = new TextToSpeak();2  string[] parameter = { richTextBox1.Text, cbbsex.Text };3  backgroundWorker1.RunWorkerAsync(parameter);

Careful friends should have discovered _ ttspeak = new texttospeak (), which is a strange instantiation statement. Similar to texttospeak, the instantiated class object is declared as a member variable. I didn't pay attention to it at the beginning. When running the program, the second time I clicked the read button, I did not respond. It turns out that synthesis resources have been released during the first run. So we need to instantiate one, that is, rewrite the member variable.

It is convenient to use the pause and resume functions.

 

1 _ttSpeak.SpeakPause();

 

_ttSpeak.SpeakResume();

Note that you must disable the read button when executing the read-only function. Otherwise, an error will be reported when you click it again.

Write the enabled method in backgroundworkerappsrunworkercompleted ~~~

 

 

 

 

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.