【发布时间】:2017-02-21 17:20:20
【问题描述】:
我打算用 C# 制作一个表单应用程序,它可以识别语音,然后用户可以说一些简单的命令,例如“hi”,计算机会回复“hi there, can I help you with someting”。我 100% 确定我的麦克风可以正常工作,但是当我调试程序时,它似乎无法识别来自我的麦克风的任何命令或音频。我正在使用 Visual Studio Community 2015。我想知道,我有什么遗漏或做错了吗?计算机加载表格并与我交谈,但它无法识别我的声音。我将不胜感激任何帮助。谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
namespace VoiceHost
{
public partial class Form1 : Form
{
SpeechSynthesizer Host = new SpeechSynthesizer();
Choices services = new Choices();
public Form1()
{
SpeechRecognitionEngine UserRecog = new SpeechRecognitionEngine();
services.Add(new string[] {"hi"});
Grammar gr = new Grammar(new GrammarBuilder(services));
Say("hello, my name is voice host, how can I help");
try
{
UserRecog.RequestRecognizerUpdate();
UserRecog.LoadGrammar(gr);
UserRecog.SpeechRecognized += UserRecog_SpeechRecognized;
UserRecog.SetInputToDefaultAudioDevice();
UserRecog.RecognizeAsync(RecognizeMode.Multiple);
}
catch { return; }
}
public void Say(string responseMessage)
{
Host.Speak(responseMessage);
}
private void UserRecog_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string userOrder = e.Result.Text;
if (userOrder == "hi")
{
Say("hi there, can I help you with someting");
}
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
【问题讨论】:
-
你做错的第一件事是
catch { return; };隐藏错误,当你编程时,你需要了解错误。 -
什么是好方法?只是不要捕获异常,然后对它们不做任何事情。至少记录它们。
-
捕获异常并查看 .Message 和 innerException.Message.. 或记录它,或显示.. 只是不要忽略它
-
例如 catch (Exception e) { MsgBox.Show(e.ToString()); }
标签: c# visual-studio visual-studio-2015 speech-recognition speech-synthesis