经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Windows » 查看文章
C# windows语音识别与朗读实例
来源:jb51  时间:2021/7/26 14:07:21  对本文有异议

C# windows语音识别与朗读示例,供大家参考,具体内容如下

本示例通过windows语音识别功能进行语音识别和文本朗读。

打开windows麦克风,点击start按键,大声朗读 “中国”、“美国”、“英国”,识别成功将发出“嘟”的提示音并朗读对应结果。

用到的语音识别模块包括:

  1. using System.Speech.Recognition;
  2. using System.Speech.Synthesis;

动态连接库文件在我的资源中下载.System.Speach.dll

示例界面如下:

程序源码如下:

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Speech.Recognition;
  4. using System.Speech.Synthesis;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7.  
  8. namespace Test
  9. {
  10. public partial class FormVoiceControl : Form
  11. {
  12. static SpeechSynthesizer SS = new SpeechSynthesizer();
  13. private SpeechRecognitionEngine SRE = new SpeechRecognitionEngine(); //语音识别模块
  14. private bool SRE_listening = false;
  15. private int wordid;
  16. private string shibie;
  17.  
  18. [DllImport("kernel32.dll")]
  19. public static extern bool Beep(int freq, int duration);
  20.  
  21. public FormVoiceControl()
  22. {
  23. InitializeComponent();
  24. }
  25.  
  26. public void InitVoice() //语音识别初始化
  27. {
  28. //SS.SelectVoice("lily");
  29. SRE.SetInputToDefaultAudioDevice(); // 默认的语音输入设备,也可以设定为去识别一个WAV文
  30.  
  31. GrammarBuilder GB = new GrammarBuilder();
  32.  
  33. GB.Append(new Choices(new string[] { "中国", "美国", "英国"}));
  34.  
  35. DictationGrammar DG = new DictationGrammar();
  36.  
  37. Grammar G = new Grammar(GB);
  38.  
  39. G.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(G_SpeechRecognized); //注册语音识别事件
  40.  
  41. SRE.EndSilenceTimeout = TimeSpan.FromSeconds(2);
  42.  
  43. SRE.LoadGrammar(G);
  44.  
  45. }
  46.  
  47. void G_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  48. {
  49. Beep(500, 500);//已识别提示音
  50.  
  51. string result = e.Result.Text;
  52. switch (result)
  53. {
  54. case "中国":
  55. shibie = "中国:五星红旗";
  56. choice(0);
  57. break;
  58. case "美国":
  59. shibie = "美国:星条旗";
  60. choice(1);
  61. break;
  62. case "英国":
  63. shibie = "英国:米字旗";
  64. choice(2);
  65. break;
  66. }
  67.  
  68. }
  69. private void Button1_Click(object sender, EventArgs e)
  70. {
  71. if (SRE_listening == false)
  72. {
  73. button1.Text = "stop";
  74. SRE.RecognizeAsync(RecognizeMode.Multiple);
  75. }
  76. else
  77. {
  78. button1.Text = "start";
  79. SRE.RecognizeAsyncStop();
  80. }
  81. lblanswer.Text = "";
  82. SRE_listening = !SRE_listening;
  83. }
  84.  
  85. private void choice(int id)
  86. {
  87. wordid = id;
  88.  
  89. Thread t1;
  90. Thread t2;
  91.  
  92. t1 = new Thread(new ThreadStart(ShowAnswer));
  93. t1.Start();
  94. t1.Join();
  95. t2 = new Thread(new ThreadStart(SpeekAnswer));
  96. t2.Start();
  97. }
  98. void ShowAnswer() //线程
  99. {
  100. MethodInvoker mi = new MethodInvoker(this.dosomething);
  101. this.BeginInvoke(mi);
  102.  
  103. }
  104. void dosomething()
  105. {
  106. lblanswer.Text = shibie;
  107. }
  108. void SpeekAnswer() //线程
  109. {
  110. switch (wordid)
  111. {
  112. case 0:
  113. SS.Speak("五星红旗");
  114. break;
  115. case 1:
  116. SS.Speak("星条旗");
  117. break;
  118. case 2:
  119. SS.Speak("米字旗");
  120. break;
  121. }
  122. }
  123.  
  124. private void FormVoiceControl_Load(object sender, EventArgs e)
  125. {
  126. InitVoice();
  127.  
  128. }
  129.  
  130.  
  131. }
  132. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号