代码改变世界

C#中英文混合朗读

2010-03-01 08:57  杨延成  阅读(3120)  评论(4编辑  收藏  举报

偶尔的项目需求里,要求可以应用程序朗读一段文字,如朗读验证码功能,这里做了一个简单的实现。

首先要引用一个类库SpeechLib.dll,具体代码如下:(本段程序是用winform实现的)

代码
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 /// 应用程序的主入口点。
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(string.Empty, string.Empty).Item(1);
41 }
42 private void SpeakChina(string strSpeak)
43 {
44 SetChinaVoice();
45 Speak(strSpeak);
46 }
47 private void SpeakEnglishi(string strSpeak)
48 {
49 SetEnglishVoice();
50 Speak(strSpeak);
51 }
52
53 public void AnalyseSpeak(string strSpeak)
54 {
55 int iCbeg = 0;
56 int iEbeg = 0;
57 bool IsChina = true;
58 for (int i = 0; i < strSpeak.Length; i++)
59 {
60 char chr = strSpeak[ i ];
61 if (IsChina)
62 {
63 if (chr <= 122 && chr >= 65)
64 {
65 int iLen = i - iCbeg;
66 string strValue = strSpeak.Substring(iCbeg, iLen);
67 SpeakChina(strValue);
68 iEbeg = i;
69 IsChina = false;
70 }
71 }
72 else
73 {
74 if (chr > 122 || chr < 65)
75 {
76 int iLen = i - iEbeg;
77 string strValue = strSpeak.Substring(iEbeg, iLen);
78 this.SpeakEnglishi(strValue);
79 iCbeg = i;
80 IsChina = true;
81 }
82 }
83
84 }//end for
85 if (IsChina)
86 {
87 int iLen = strSpeak.Length - iCbeg;
88 string strValue = strSpeak.Substring(iCbeg, iLen);
89 SpeakChina(strValue);
90 }
91 else
92 {
93 int iLen = strSpeak.Length - iEbeg;
94 string strValue = strSpeak.Substring(iEbeg, iLen);
95 SpeakEnglishi(strValue);
96 }
97
98 }
99 private void BuildSpeach()
100 {
101 if (voice == null)
102 voice = new SpVoiceClass();
103 }
104 public int Volume
105 {
106 get
107 {
108 return voice.Volume;
109 }
110 set
111 {
112 voice.SetVolume((ushort)(value));
113 }
114 }
115 public int Rate
116 {
117 get
118 {
119 return voice.Rate;
120 }
121 set
122 {
123 voice.SetRate(value);
124 }
125 }
126 private void Speak(string strSpeack)
127 {
128 try
129 {
130 voice.Speak(strSpeack, SpeechVoiceSpeakFlags.SVSFlagsAsync);
131 }
132 catch (Exception err)
133 {
134 throw (new Exception("发生一个错误:" + err.Message));
135 }
136 }
137
138 public void Stop()
139 {
140 voice.Speak(string.Empty, SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
141 }
142 public void Pause()
143 {
144 voice.Pause();
145 }
146 public void Continue()
147 {
148 voice.Resume();
149 }
150
151
152 }//end class
153
154 }
155
156
157 //Form1.cs
158
159 using System;
160 using System.Collections.Generic;
161 using System.ComponentModel;
162 using System.Data;
163 using System.Drawing;
164 using System.Text;
165 using System.Windows.Forms;
166 using DotNetSpeech;
167 namespace TestSpeaker1
168 {
169 public partial class Form1 : Form
170 {
171 public Form1()
172 {
173 InitializeComponent();
174 }
175 private void Form1_Load(object sender, EventArgs e)
176 {
177 }
178 private void button1_Click(object sender, EventArgs e)
179 {
180 Speach sp = Speach.instance();
181 sp.Volume = 100;
182 sp.Rate = 1;
183 sp.AnalyseSpeak(this.SpeakerText.Text.Trim());
184 //try
185 //{
186 // SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
187 // SpVoice Voice = new SpVoice();
188 // ///3表示是汉用,0124都表示英语,就是口音不同
189 // Voice.Voice = Voice.GetVoices(string.Empty, string.Empty).Item(0);
190 // //voice.Voice =voice.GetVoices(string.Empty, string.Empty).Item(0);
191 // Voice.Speak(this.textBox1.Text, SpFlags);
192 //}
193 //catch (Exception er)
194 //{
195 // MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
196 //}
197 }
198 private void button2_Click(object sender, EventArgs e)
199 {
200 try
201 {
202 SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
203 SpVoice Voice = new SpVoice();
204 SaveFileDialog sfd = new SaveFileDialog();
205 sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
206 sfd.Title = "Save to a wave file";
207 sfd.FilterIndex = 2;
208 sfd.RestoreDirectory = true;
209 if (sfd.ShowDialog() == DialogResult.OK)
210 {
211 SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
212 SpFileStream SpFileStream = new SpFileStream();
213 SpFileStream.Open(sfd.FileName, SpFileMode, false);
214 Voice.AudioOutputStream = SpFileStream;
215 Voice.Speak(this.SpeakerText.Text, SpFlags);
216 Voice.WaitUntilDone(100);
217 SpFileStream.Close();
218 }
219 }
220 catch (Exception er)
221 {
222 MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
223 }
224 }
225 }
226 }