我使用Visual Studio在C ++中制作了语音识别程序。在我使用Windows语音识别培训师之前它运行没有问题,现在程序运行但它没有说什么或响应我的命令。 这是该计划:
using System;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Diagnostics;
using System.Xml;
using System.Media;
namespace JarvisTest
{
public partial class Form1 : Form
{
SpeechSynthesizer s = new SpeechSynthesizer();
Boolean wake = false;
String name = "Rhys";
String temp;
String condition;
String high;
String low;
Choices list = new Choices();
public Form1()
{
SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
list.Add(new String[] { "hello", "bye", "how are you", "great", "what time is it", "whats the date", "open google", "open youtube", "wake", "sleep", "restart", "update", "whats the weather", "whats the temperature", "jarvis", "shut down", "exit", "play", "pause", "spotify", "last track", "next track", "whats todays high", "whats todays low", "whats todays weather", "whats your favorite song", "never mind", "whats my name", "minimise", "maximise"});
Grammar gr = new Grammar(new GrammarBuilder(list));
try
{
rec.RequestRecognizerUpdate();
rec.LoadGrammar(gr);
rec.SpeechRecognized += rec_SpeachRecognized;
rec.SetInputToDefaultAudioDevice();
rec.RecognizeAsync(RecognizeMode.Multiple);
}
catch { return; }
s.SelectVoiceByHints(VoiceGender.Male);
s.Speak("hello, how may i assist you?");
InitializeComponent();
}
public String GetWeather(String input)
{
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=' Guernsey')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
XmlDocument wData = new XmlDocument();
try
{
wData.Load(query);
}
catch
{
//MessageBox.Show("No internet connection");
return "No internet connection detected";
}
XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("query").SelectSingleNode("results").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("query/results/channel");
try
{
int rawtemp = int.Parse(channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value);
int rawhigh = int.Parse(channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["high"].Value);
int rawlow = int.Parse(channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["low"].Value);
temp = (rawtemp - 32) * 5/9 + "";
high = (rawhigh - 32) * 5 / 9 + "";
low = (rawlow - 32) * 5 / 9 + "";
condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;
if (input == "temp")
{
return temp;
}
if (input == "high")
{
return high;
}
if (input == "low")
{
return low;
}
if (input == "cond")
{
return condition;
}
}
catch
{
return "Error Reciving data";
}
return "error";
}
public void restart()
{
Process.Start(@"C:\Users\Rhys-Le-P\Documents\Visual Studio 2017\Projects\JarvisTest\JarvisTest\obj\Debug\JarvisTest.exe");
Environment.Exit(1);
}
public void shutdown()
{
Environment.Exit(1);
}
public void say(String h)
{
s.Speak(h);
wake = false;
}
String[] greetings = new String[3] { "hi", "hello", "hi, how are you" };
public String greetings_action()
{
Random r = new Random();
return greetings[r.Next(3)];
}
//Commands
private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
{
String r = e.Result.Text;
if (r == "jarvis")
{
wake = true;
//say(greetings_action());
SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Rhys-Le-P\Downloads\Beep.wav");
simpleSound.Play();
}
// if (r == "wake")
// {
// wake = true;
// say("i am awake");
// }
//if (r == "sleep")
//{
// wake = false;
// say("zzz");
// }
if (wake == true)
{
if (r == "minimise")
{
this.WindowState = FormWindowState.Minimized;
}
if (r == "maximise")
{
this.WindowState = FormWindowState.Maximized;
}
if (r == "whats my name")
{
say("your name is " + name);
}
if (r == "next track")
{
SendKeys.Send("^{RIGHT}");
//say("playing next track");
}
if(r == "last track")
{
SendKeys.Send("^{LEFT}");
//say("playing last track");
}
if(r == "spotify")
{
Process.Start(@"C:\Users\Rhys-Le-P\AppData\Roaming\Spotify\Spotify.exe");
}
//{
// input
if (r == "play" || r == "pause")
{
SendKeys.Send(" ");
}
// input
if (r == "hello")
{
//output
say("hi");
}
// input
if (r == "how are you")
{
//output
say("good, and you?");
}
// input
if (r == "bye")
{
//output
say("good bye!");
}
// input
if (r == "great")
{
//output
say("good to hear!");
}
// input
if (r == "what time is it")
{
//output
say(DateTime.Now.ToString("h:mm tt"));
}
// input
if (r == "whats the date")
{
//output
say(DateTime.Now.ToString("d/M/yyyy"));
}
// input
if (r == "open google")
{
//output
say("opening google");
Process.Start("http://www.google.com");
}
// input
if (r == "open youtube")
{
//output
say("opening youtube");
Process.Start("http://www.youtube.com");
}
// input
if (r == "restart" || r == "update")
{
//output
say("restarting");
restart();
}
if (r == "whats todays weather")
{
say("Todays weather is, " + GetWeather("cond") + "with a high of" + GetWeather("high") + "degrees and a low of" + GetWeather("low") +"degrees");
}
if (r == "whats the weather")
{
say("The sky is, " + GetWeather("cond") + ".");
}
if (r == "whats the temperature")
{
say("it is, " + GetWeather("temp") + "degrees.");
}
if (r == "whats todays high")
{
say("Todays high is, " + GetWeather("high") + "degrees.");
}
if (r == "whats todays low")
{
say("Todays low is, " + GetWeather("low") + "degrees.");
}
// input
if (r == "shut down" || r == "exit")
{
//output
say("Shutting Down System");
shutdown();
}
if (r == "whats your favorite song")
{
say("I love this song!");
SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Rhys-Le-P\Downloads\BSIM.wav");
simpleSound.Play();
}
if (r == "never mind")
{
say("Ok sir");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
有没有人知道如何解决这个问题。
由于