In occasional project requirements, the application is required to read a piece of text, such as the verification code reading function. Here is a simple implementation.
First, you must reference a class library SpeechLib. dll. The specific code is as follows: (this program is implemented using winform)
Code
1 using System;
2 using System. Collections. Generic;
3 using System. Windows. Forms;
4 using SpeechLib;
5 namespace TestSpeaker1
6 {
7 static class Program
8 {
9 /// <summary>
10 /// main entry point of the application.
11 /// </summary>
12 [STAThread]
13 static void Main ()
14 {
15 Application. EnableVisualStyles ();
16 Application. SetCompatibleTextRenderingDefault (false );
17 Application. Run (new Form1 ());
18}
19}
20 public class Speach
21 {
22 private static Speach _ Instance = null;
23 private SpeechLib. SpVoiceClass voice = null;
24 private Speach ()
25 {
26 BuildSpeach ();
27}
28 public static Speach instance ()
29 {
30 if (_ Instance = null)
31 _ Instance = new Speach ();
32 return _ Instance;
33}
34 private void SetChinaVoice ()
35 {
36 voice. Voice = voice. GetVoices (string. Empty, string. Empty). Item (3 );
37}
38 private void SetEnglishVoice ()
39 {
40 voice. Voice = voice. GetVoices (