1. Open Vs and create a new form project
2. Add reference: Microsoft Speech Object Library Reference on the com Tab
Add references in the code area.
Using speechlib;
Using system. Threading; // This reference is a thread class used to save voice files.
3. Add two buttons in the text box
4. Enter the following code under a button:
Speechvoicespeakflags flag = speechvoicespeakflags. svsflagsasync;
Spvoice voice = new spvoice ();
Voice. Voice = voice. getvoices (string. Empty, String. Empty). Item (3 );
// Item (0) Word male Sam
// Item (1) Male Mike
// Item (2) Word Female Mary
// Chinese pronunciation of Item (3). If it is English, it is pronounced one by letter.
Voice. Speak (textBox1.Text, flag );
Code explanation: the most important thing is that the SPVoice class is the core of this DEMO. Then, the annotations below the Item (3) option clearly explain what it means. There is another SPEAK method later. I don't need to say it. I know what it means! Of course, there are still many ways to check the SpVoice object. The Microsoft Speech API also has pause/ResumeSetVoice/GetVoiceSetRate|SetRateSetVolume/GetVolumeAccording to his meaning, these methods know more about how to pause and continue to set the sound reading speed. For more object methods, refer to his API
5. Enter the following code in another button:
SpeechVoiceSpeakFlags flag = SpeechVoiceSpeakFlags. SVSFlagsAsync;
SpVoice voice = new SpVoice ();
Voice. Voice = voice. getvoices (string. Empty, String. Empty). Item (3 );
Speechstreamfilemode spfilemode = speechstreamfilemode. ssfmcreateforwrite;
Spfilestream = new spfilestream ();
Spfilestream. Open (@ "C:/test.wav", spfilemode, false );
Voice. audiooutputstream = spfilestream; // set the output of voice to stream.
Voice. Speak (textbox1.text. Trim (), flag );
Voice. WaitUntilDone (Timeout. Infinite); // Using System. Threading;
SpFileStream. Close ();
In fact, this code is similar to the previous Code. It just saves the sound as a voice file through a stream.