C# 实现语音播报语音识别
代码如下:添加引用:System.Speechpublic static void Speaking(string saying){string say = saying;Task task = new Task(() =>{SpeechSynthesizer speech = new SpeechSynthesizer();
·
代码如下:
语音播报
添加引用:System.Speech.Synthesis或Microsoft.Speech.Synthesis
public static void Speaking(string saying)
{
string say = saying;
Task task = new Task(() =>
{
SpeechSynthesizer speech = new SpeechSynthesizer();
speech.Volume = 100; //音量
speech.SetOutputToDefaultAudioDevice();//设定默认音频设备
CultureInfo keyboardCulture = System.Windows.Forms.InputLanguage.CurrentInputLanguage.Culture;//获取当前输入语言的区域性
InstalledVoice neededVoice = speech.GetInstalledVoices(keyboardCulture).FirstOrDefault();
if (neededVoice == null)
{
say = "未知的操作";
}
else
{
speech.SelectVoice(neededVoice.VoiceInfo.Name);
}
string str = say + "," + say + "," + say;
speech.SpeakAsync(str);//异步播报
});
task.Start();
}
语音识别
添加引用:System.Speech.Recognition
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-CN"));//初始化中文引擎
Choices conmmonds = new Choices();
conmmonds.Add(new string[] { "导通", "断开" });//添加词条
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(conmmonds);
Grammar grammar = new Grammar(gBuilder);
recognizer.LoadGrammar(grammar);
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Deal_SpeechRecongized);
recognizer.SetInputToDefaultAudioDevice();//设置默认输入设备
recognizer.RecognizeAsync(RecognizeMode.Multiple);//开始监控设备输入准备识别
static void Deal_SpeechRecongized(object sender, SpeechRecognizedEventArgs e) //处理识别结果
{
Console.WriteLine("识别结果:" + e.Result.Text);
}
Windows7或XP系统语音播报会报BUG如图所下:
检索COM类工厂中CLSID为{D9F6EE60-58C9-458B-88E1-2F908FD7F87C}的组件失败,原因是出现以下错误:8007007e找不到指定的模块错误
解决方案:
- 在项目引用中选中System.Speech,右键打开属性,将嵌入互操作类型及特定版本选为False,如下图所示
- 检查系统存不存在语音识别模块,一部分Windows7系统可能由于安装系统时去除了一部分包括语音识别功能组件,导致sapi.dll文件不存在,所以就会爆出上述错误,没有讲述人语言包。
- 打开控制面板,切换为小图标,正常会看到语音识别图标,而功能缺乏的系统会找不到语音识别
- 如果没有则需要下载Windows7系统TTS修复,下载链接在文章底部,csdn 1积分下载,无积分上传审核不通过
- 需要注意的是即使下载修复软件进行修复后,控制面板中出现了语音识别图标,但运行语音识别软件后,但可能会爆出“无法创建所需的引擎ID”类似错误,这时需要将麦克风插入计算机中,然后重启软件,如果依然存在问题,说明修复未成功,Debug失败
更多推荐
已为社区贡献1条内容
所有评论(0)