1

我正在基于语音识别创建类似于钢铁侠 JARVIS 的程序。我在使用这个之前做了一个:

case "Open facebook":
    JARVIS.Speak("Opening facebook");
    Process.Start("www.facebook.com");
    break;`

但现在我想创建搜索选项甚至玩。到目前为止,我为 youtube 搜索做了这个(一个语法并且效果很好)但是当我做了两个时:

public partial class Form1 : Form
{
    SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
    SpeechSynthesizer JARVIS = new SpeechSynthesizer();
    string QEvent;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Choices artists = new Choices(new string[] { "bullet-for-my-valentine-curses", "black-veil-brides-saviour", "three-days-grace-wake-up" });
        Choices search = new Choices(new string[] { "bill-gates" });

        GrammarBuilder findServices = new GrammarBuilder("Play");
        findServices.Append(artists);
        GrammarBuilder google = new GrammarBuilder("Look");
        google.Append("for");
        google.Append(search);

        // Create a Grammar object from the GrammarBuilder and load it to the recognizer.
        Grammar servicesGrammar = new Grammar(findServices);
        Grammar lookingGrammar = new Grammar(google);
        _recognizer.RequestRecognizerUpdate();
        _recognizer.LoadGrammarAsync(servicesGrammar);
        _recognizer.LoadGrammarAsync(lookingGrammar);

        // Add a handler for the speech recognized event.
        _recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);

        // Configure the input to the speech recognizer.
        _recognizer.SetInputToDefaultAudioDevice();

        // Start asynchronous, continuous speech recognition.
        _recognizer.RecognizeAsync(RecognizeMode.Multiple);
    }
    void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        JARVIS.Speak("Playing" + e.Result.Words[1].Text);
        Process.Start("http://www.youtube.com/results?search_query=" + e.Result.Words[1].Text);
        JARVIS.Speak("Searching" + e.Result.Words[2].Text + " " + e.Result.Words[3].Text);
    }

}

我在哪里说“播放”和歌曲的标题,它会为我所说的打开选择(艺术家)。并且使用一种语法,但是当我创建两个语法时,就像上面的代码一样,当我说些什么时,程序会停止并显示错误。

{
    static class Program
    {

        // The main entry point for the application.
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1()); //this line show error
        }
    }
}

那么如何制作多种语法,一种用于 Google 搜索,一种用于 Wikipedia,并保留这个?

4

1 回答 1

2

我要借用Raymond Chen的Psychic Debugging Talents (tm) 说你的问题出在这里:

_recognizer.LoadGrammarAsync(servicesGrammar);
_recognizer.LoadGrammarAsync(lookingGrammar);

特别是,我怀疑识别器一次只能加载一个异步语法。如果您将代码更改为

_recognizer.LoadGrammar(servicesGrammar);
_recognizer.LoadGrammar(lookingGrammar);

或将第二个LoadGrammarAsync调用放在onLoadGrammarCompleted处理程序中,您的问题就会消失。

严重的是,您需要包含错误。

于 2013-07-22T02:11:07.557 回答