1

音声認識に基づいて、アイアンマンの JARVIS に似たプログラムを作成しています。これを使用する前に作成しました:

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

しかし、今は検索オプションを作成したり、プレイしたりしたいと思っています。これまでのところ、YouTube検索用にこれを作成しました(1つの文法でうまく機能します)が、2つ作成すると:

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);
    }

}

「再生」と曲のタイトルを言うと、私が言ったことの選択肢 (アーティスト) が開きます。そして、1 つの文法で動作しますが、上記のコードのように 2 つ作成すると、何かを言うと、プログラムが停止してエラーが表示されます。

{
    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 検索用とウィキペディア用の複数の文法を作成し、これを保持するにはどうすればよいでしょうか?

4

1 に答える 1

2

Raymond Chen の Psychic Debugging Talents (tm) を借りて、あなたの問題はここにあると言います。

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

特に、レコグナイザーは一度に 1 つの非同期文法しかロードできないのではないかと思います。コードを次のように変更すると

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

または、2 番目のLoadGrammarAsync呼び出しをonLoadGrammarCompletedハンドラーに配置すると、問題は解消されます。

しかし、真剣に、エラーを含める必要があります。

于 2013-07-22T02:11:07.557 に答える