System. Speech. Synthesis: adds the pause and resume functions. system. speech. dll

Source: Internet
Author: User

System. Speech. Synthesis: adds the pause and resume functions. system. speech. dll

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 ~~~

 

 

 

 


C # single-key pause can be int I = 1; // first define the member variable I; 0 indicates start, 1 indicates pause
// The famous button1Button Control
Private void button#click (object sender, EventArgs e)
{
If (I = 1)
{
// MessageBox. Show ("START ");
// Write the code to implement the function
This. I = 0;
Return;
}
If (I = 0)
{
// MessageBox. Show ("paused ");
// Write the code to implement the function
This. I = 1;
Return;
}
}
The java button can be used to pause and continue. I will write a Demo:

Import java. awt. event .*;
Import javax. swing .*;

Public class Demo extends Thread {
Private JFrame frm = new JFrame ("Demo ");
Private JButton btnPause = new JButton ("Pause ");
Private JLabel lblTest = new JLabel ("0 ");
Private boolean isPaused = false;
Private int seconds = 0;

Public void run (){
While (true ){
While (isPaused );
Seconds ++;
LblTest. setText (seconds + "");
Try {
Sleep (200 );
} Catch (InterruptedException ie ){}
}
}

Public Demo (){
Frm. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Frm. setResizable (false );
Frm. setLayout (null );
Frm. setSize (150,200 );
Frm. setLocationRelativeTo (null );

LblTest. setBounds (20, 50,100, 25 );

BtnPause. setBounds (20,120,100, 30 );
BtnPause. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent AE ){
If (isPaused ){
BtnPause. setText ("Pause ");
IsPaused = false;
} Else {
BtnPause. setText ("Resume ");
IsPaused = true;
}
}
});

Frm. add (lblTest );
Frm. add (btnPause );
Frm. setVisible (true );
Start ();
}

Public static void main (String args []) {
New Demo ();
}
}

Related Article

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.