各位大神好,我现在需要一个C#的文本转语音的demo,谁能提供一个供我学习学习?谢谢了。
实现功能如:银行窗口叫号功能。“请张三到3号窗口办理业务”
首先引用 System.Speech
以下是Winform完整代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Speech.Synthesis; 10 using System.IO; 11 12 namespace text_to_speech 13 { 14 public partial class Form1 : Form 15 { 16 SpeechSynthesizer reader; 17 public Form1() 18 { 19 InitializeComponent(); 20 21 } 22 23 #region 窗体对象 24 private System.Windows.Forms.TextBox textBox1; 25 private System.Windows.Forms.Button button1; 26 private System.Windows.Forms.Button button2; 27 private System.Windows.Forms.Button button3; 28 private System.Windows.Forms.Button button4; 29 private System.Windows.Forms.Label label3; 30 private System.Windows.Forms.Label label2; 31 private System.Windows.Forms.Label label1; 32 private System.Windows.Forms.OpenFileDialog openFileDialog1; 33 private System.Windows.Forms.Button button5; 34 private System.Windows.Forms.Label label4; 35 36 private void InitializeComponent() 37 { 38 this.textBox1 = new System.Windows.Forms.TextBox(); 39 this.button1 = new System.Windows.Forms.Button(); 40 this.button2 = new System.Windows.Forms.Button(); 41 this.button3 = new System.Windows.Forms.Button(); 42 this.button4 = new System.Windows.Forms.Button(); 43 this.label3 = new System.Windows.Forms.Label(); 44 this.label2 = new System.Windows.Forms.Label(); 45 this.label1 = new System.Windows.Forms.Label(); 46 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 47 this.button5 = new System.Windows.Forms.Button(); 48 this.label4 = new System.Windows.Forms.Label(); 49 this.SuspendLayout(); 50 // 51 // textBox1 52 // 53 this.textBox1.Font = new System.Drawing.Font("Times New Roman", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 54 this.textBox1.Location = new System.Drawing.Point(13, 37); 55 this.textBox1.Multiline = true; 56 this.textBox1.Name = "textBox1"; 57 this.textBox1.Size = new System.Drawing.Size(634, 213); 58 this.textBox1.TabIndex = 0; 59 // 60 // button1 61 // 62 this.button1.BackColor = System.Drawing.Color.Black; 63 this.button1.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 64 this.button1.ForeColor = System.Drawing.SystemColors.ControlLightLight; 65 this.button1.Location = new System.Drawing.Point(13, 256); 66 this.button1.Name = "button1"; 67 this.button1.Size = new System.Drawing.Size(131, 56); 68 this.button1.TabIndex = 1; 69 this.button1.Text = "SPEAK TEXT"; 70 this.button1.UseVisualStyleBackColor = false; 71 this.button1.Click += new System.EventHandler(this.button1_Click); 72 // 73 // button2 74 // 75 this.button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); 76 this.button2.Enabled = false; 77 this.button2.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 78 this.button2.ForeColor = System.Drawing.SystemColors.ActiveCaptionText; 79 this.button2.Location = new System.Drawing.Point(185, 256); 80 this.button2.Name = "button2"; 81 this.button2.Size = new System.Drawing.Size(131, 56); 82 this.button2.TabIndex = 2; 83 this.button2.Text = "PAUSE"; 84 this.button2.UseVisualStyleBackColor = false; 85 this.button2.Click += new System.EventHandler(this.button2_Click); 86 // 87 // button3 88 // 89 this.button3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); 90 this.button3.Enabled = false; 91 this.button3.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 92 this.button3.ForeColor = System.Drawing.SystemColors.ActiveCaptionText; 93 this.button3.Location = new System.Drawing.Point(350, 256); 94 this.button3.Name = "button3"; 95 this.button3.Size = new System.Drawing.Size(131, 56); 96 this.button3.TabIndex = 3; 97 this.button3.Text = "RESUME"; 98 this.button3.UseVisualStyleBackColor = false; 99 this.button3.Click += new System.EventHandler(this.button3_Click); 100 // 101 // button4 102 // 103 this.button4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); 104 this.button4.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 105 this.button4.ForeColor = System.Drawing.SystemColors.ActiveCaptionText; 106 this.button4.Location = new System.Drawing.Point(516, 256); 107 this.button4.Name = "button4"; 108 this.button4.Size = new System.Drawing.Size(131, 56); 109 this.button4.TabIndex = 4; 110 this.button4.Text = "STOP"; 111 this.button4.UseVisualStyleBackColor = false; 112 this.button4.Click += new System.EventHandler(this.button4_Click); 113 // 114 // label3 115 // 116 this.label3.AutoSize = true; 117 this.label3.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 118 this.label3.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(128))))); 119 this.label3.Location = new System.Drawing.Point(9, 11); 120 this.label3.Name = "label3"; 121 this.label3.Size = new System.Drawing.Size(204, 19); 122 this.label3.TabIndex = 7; 123 this.label3.Text = "Enter Text In The Box Below"; 124 // 125 // label2 126 // 127 this.label2.AutoSize = true; 128 this.label2.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 129 this.label2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(128))))); 130 this.label2.Location = new System.Drawing.Point(210, 344); 131 this.label2.Name = "label2"; 132 this.label2.Size = new System.Drawing.Size(47, 19); 133 this.label2.TabIndex = 9; 134 this.label2.Text = "IDLE"; 135 // 136 // label1 137 // 138 this.label1.AutoSize = true; 139 this.label1.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 140 this.label1.Location = new System.Drawing.Point(12, 344); 141 this.label1.Name = "label1"; 142 this.label1.Size = new System.Drawing.Size(201, 19); 143 this.label1.TabIndex = 8; 144 this.label1.Text = "SYNTHESIZER STATUS : "; 145 // 146 // openFileDialog1 147 // 148 this.openFileDialog1.FileName = "openFileDialog1"; 149 this.openFileDialog1.Filter = "\"Text Files(*.txt)|*.txt"; 150 this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk); 151 // 152 // button5 153 // 154 this.button5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F); 155 this.button5.Location = new System.Drawing.Point(440, 11); 156 this.button5.Name = "button5"; 157 this.button5.Size = new System.Drawing.Size(207, 24); 158 this.button5.TabIndex = 10; 159 this.button5.Text = "Load Data From A Text File"; 160 this.button5.UseVisualStyleBackColor = true; 161 this.button5.Click += new System.EventHandler(this.button5_Click); 162 // 163 // label4 164 // 165 this.label4.AutoSize = true; 166 this.label4.Font = new System.Drawing.Font("Times New Roman", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 167 this.label4.ForeColor = System.Drawing.Color.Gold; 168 this.label4.Location = new System.Drawing.Point(472, 328); 169 this.label4.Name = "label4"; 170 this.label4.Size = new System.Drawing.Size(173, 45); 171 this.label4.TabIndex = 11; 172 this.label4.Text = "Demo By :\r\nSaumitra Srivastav\r\nsaumitra.srivastav7@gmail.com"; 173 // 174 // Form1 175 // 176 this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); 177 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 178 this.BackColor = System.Drawing.Color.Gray; 179 this.ClientSize = new System.Drawing.Size(657, 381); 180 this.Controls.Add(this.label4); 181 this.Controls.Add(this.button5); 182 this.Controls.Add(this.label2); 183 this.Controls.Add(this.label1); 184 this.Controls.Add(this.label3); 185 this.Controls.Add(this.button4); 186 this.Controls.Add(this.button3); 187 this.Controls.Add(this.button2); 188 this.Controls.Add(this.button1); 189 this.Controls.Add(this.textBox1); 190 this.Font = new System.Drawing.Font("MS Outlook", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2))); 191 this.Name = "Form1"; 192 this.Text = "TEXT-TO-SPEECH CONVERTER"; 193 this.Load += new System.EventHandler(this.Form1_Load); 194 this.ResumeLayout(false); 195 this.PerformLayout(); 196 197 } 198 199 200 201 202 /// <summary> 203 /// Required designer variable. 204 /// </summary> 205 private System.ComponentModel.IContainer components = null; 206 207 /// <summary> 208 /// Clean up any resources being used. 209 /// </summary> 210 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 211 protected override void Dispose(bool disposing) 212 { 213 if (disposing && (components != null)) 214 { 215 components.Dispose(); 216 } 217 base.Dispose(disposing); 218 } 219 220 #endregion 221 222 223 private void Form1_Load(object sender, EventArgs e) 224 { 225 reader = new SpeechSynthesizer(); 226 button2.Enabled = false; 227 button3.Enabled = false; 228 button4.Enabled = false; 229 textBox1.ScrollBars = ScrollBars.Both; 230 } 231 232 //SPEAK TEXT 233 private void button1_Click(object sender, EventArgs e) 234 { 235 reader.Dispose(); 236 if (textBox1.Text != "") 237 { 238 239 reader = new SpeechSynthesizer(); 240 reader.SpeakAsync(textBox1.Text); 241 label2.Text = "SPEAKING"; 242 button2.Enabled = true; 243 button4.Enabled = true; 244 reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(reader_SpeakCompleted); 245 } 246 else 247 { 248 MessageBox.Show("Please enter some text in the textbox", "Message", MessageBoxButtons.OK); 249 } 250 } 251 252 void reader_SpeakCompleted(object sender, SpeakCompletedEventArgs e) 253 { 254 label2.Text = "IDLE"; 255 } 256 257 //PAUSE 258 private void button2_Click(object sender, EventArgs e) 259 { 260 if (reader != null) 261 { 262 if (reader.State == SynthesizerState.Speaking) 263 { 264 reader.Pause(); 265 label2.Text = "PAUSED"; 266 button3.Enabled = true; 267 268 } 269 } 270 } 271 272 //RESUME 273 private void button3_Click(object sender, EventArgs e) 274 { 275 if (reader != null) 276 { 277 if (reader.State == SynthesizerState.Paused) 278 { 279 reader.Resume(); 280 label2.Text = "SPEAKING"; 281 } 282 button3.Enabled = false; 283 } 284 } 285 286 private void button4_Click(object sender, EventArgs e) 287 { 288 if (reader != null) 289 { 290 reader.Dispose(); 291 label2.Text = "IDLE"; 292 button2.Enabled = false; 293 button3.Enabled = false; 294 button4.Enabled = false; 295 } 296 } 297 298 private void button5_Click(object sender, EventArgs e) 299 { 300 openFileDialog1.ShowDialog(); 301 } 302 303 private void openFileDialog1_FileOk(object sender, CancelEventArgs e) 304 { 305 textBox1.Text = File.ReadAllText(openFileDialog1.FileName.ToString()); 306 307 } 308 } 309 }
能给我上一张界面图片么?
@zhuweirandy: 点击第一个按钮没反应?
@勿忘心安2: 自己好好看下代码,全都给你了,除非你压根没仔细看
@zhuweirandy: 这个它是通过调取操作系统内部的语音库来实现的,读起来还是显得有些生硬。
@勿忘心安2: 功能已经实现,如果需要其他语音,可能需要安装语音包
去搜下讯飞