我打算在C#中創建一個表單應用程序,用於識別語音,然後用戶可以說出一些簡單的命令,例如「hi」,並且計算機會以「hi there,can我幫助你做些什麼「。我100%確定我的麥克風工作正常,但是當我調試程序時,它似乎無法識別來自麥克風的任何命令或音頻。我正在使用Visual Studio Community 2015.我想知道,有什麼我錯過了或做錯了嗎?電腦載入表格並與我交談,但它不能識別我的聲音。我將不勝感激任何幫助。謝謝!System.Speech在調試過程中無法識別語音
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; }';隱藏錯誤,當你編程時,你需要知道錯誤。 –
你是什麼意思「什麼是好方法」?只是不要捕捉異常,然後對它們無所作爲。至少記錄他們。 – mason
捕獲異常,並在.Message和innerException.Message ..中查找或記錄它,或顯示..只是不要忽略它 –