偶爾的項目需求裡,要求可以應用程序朗讀一段文字,如朗讀驗證碼功能,這裡做了一個簡單的實現。
首先要引用一個類庫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(