c# - C#语音识别不会回答我
问题描述
我做了这个简单的代码,但它似乎无法识别我的声音。我尝试了从使用另一个输入(麦克风)到读取异常的所有方法。它总是直接说“出了点问题”。有人可以检查我的代码是否有错误吗?或者如果你有同样的问题,我该如何解决?我也在互联网上搜索了一段时间,但我找不到正确的答案。
代码:
using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Threading;
namespace voice_bot
{
public partial class Form1 : Form
{
SpeechSynthesizer s = new SpeechSynthesizer();
Choices list = new Choices();
public Form1()
{
SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
list.Add(new String[] { "hello", "how are you" });
Grammar gr = new Grammar(new GrammarBuilder(list));
s.Speak("hello, my name is voice bot. If you would like to talk with me, go ahead");
try
{
rec.RequestRecognizerUpdate();
rec.LoadGrammar(gr);
rec.SpeechRecognized += rec_SpeachRecognized;
rec.SetInputToDefaultAudioDevice();
rec.RecognizeAsync(RecognizeMode.Multiple);
}
catch (Exception e)
{
s.Speak("Something went wrong");
}
}
public void say(String h)
{
s.Speak(h);
}
private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
{
string r = e.Result.Text;
if (r == "hello")
{
s.Speak("hi");
}
if (r == "how are you")
{
s.Speak("good, how are you?");
}
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
解决方案
我不确定你在什么平台上尝试这个,但System.Speech
它已经很老了。
你可以试试微软最新的语音平台(注意:我在 System.Speech 和这个最新的语音平台上都工作过)。您可以在此处查看新平台:https ://aka.ms/speech/sdk 。有一堆示例应该可以很快引导您找到一个可行的解决方案,它适用于“所有”现代平台操作系统(windows、linux、android、ios、mac 等)并通过“所有”现代编程语言(c++、c#、java、javascript、python、objective-c、swift等...)
--抢劫
推荐阅读
- c++ - 第一次在 NetBeans IDE 8.2 中使用 SFML-2.5.1 我遇到了问题
- ios - 当我尝试连接到 Firebase 时,它只加载但未连接?
- python-3.x - 如何一次从 2 个列表的产品中动态创建字典
- javascript - VueJS:谷歌地图在数据准备好之前加载 - 如何让它等待?(努克斯特)
- javascript - Javascript 悬停文本
- c# - 使用作为字符串接收的 JSON 数组
- python - 从用户输入中搜索 SQLite 数据
- javascript - 如何获取react-table中单击单元格的行索引?
- git - Git pull 抛出以下错误:致命这个操作必须在工作树中运行
- react-native - 如何修复 Expo 推送通知错误:“无法获取设备的 GCM 令牌”?