System.Speech.Recognition的问题 - SpeechRecognitionEngine不接听语音

时间:2015-07-06 21:43:53

标签: c# .net windows-8 speech-recognition system.speech.recognition

我在尝试实施Microsoft提供的有关如何使用SpeechRecognitionEngine(https://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.speechdetected(v=vs.110).aspx)的示例时遇到了重大问题。

以下是示例代码:

using System;
using System.Speech.Recognition;

namespace SampleRecognition
{
class Program
{
    static void Main(string[] args)

    // Initialize an in-process speech recognition engine.
    {
        using (SpeechRecognitionEngine recognizer =
           new SpeechRecognitionEngine())
        {

            // Create a grammar.
            Choices cities = new Choices(new string[] { 
            "Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });

            GrammarBuilder gb = new GrammarBuilder();
            gb.Culture = new System.Globalization.CultureInfo("en-GB");
            gb.Append("I would like to fly from");
            gb.Append(cities);
            gb.Append("to");
            gb.Append(cities);

            // Create a Grammar object and load it to the recognizer.
            Grammar g = new Grammar(gb);
            g.Name = ("City Chooser");
            recognizer.LoadGrammarAsync(g);

            // Attach event handlers.
            recognizer.LoadGrammarCompleted +=
              new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);
            recognizer.SpeechDetected +=
              new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);
            recognizer.SpeechRecognized +=
              new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

            // Set the input to the recognizer.
            recognizer.SetInputToDefaultAudioDevice();

            // Start recognition.
            recognizer.RecognizeAsync();

            // Keep the console window open.
            Console.ReadLine();
        }
    }

    // Handle the SpeechDetected event.
    static void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)
    {
        Console.WriteLine("  Speech detected at AudioPosition = {0}", e.AudioPosition);
    }

    // Handle the LoadGrammarCompleted event.
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
    {
        Console.WriteLine("Grammar loaded: " + e.Grammar.Name);
    }

    // Handle the SpeechRecognized event.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        Console.WriteLine("  Speech recognized: " + e.Result.Text);
    }
  }
}

似乎没有任何作用,我尝试了许多不同的例子,并花了一整天努力让它发挥作用。如果我将 RecognizeAsync()替换为 EmulateRecognizeAsync(“我想从芝加哥飞往迈阿密”),例如,它会按预期工作。但似乎该程序没有从我的麦克风获得任何输入。

以下是一些更多细节:

  • Windows 8.1
  • .NET 4.0
  • 联想ThinkPad内置麦克风
  • Visual Studios 2012
  • 值得一提的是,程序的输出有加载语法时写出的信息消息:

    ConsoleApplication1.vshost.exe Information: 0 : SAPI does not implement phonetic alphabet selection.
    

我错过了什么吗?我需要更好的麦克风吗?除了将麦克风设为默认麦克风之外,我还需要设置硬件吗?我是否需要为Windows 8使用不同的库?我没有想法。

先谢谢你了!

1 个答案:

答案 0 :(得分:1)

结果 - 笔记本电脑内置的麦克风根本不能用于Windows语音识别......

我今天刚收到一个新耳机,一切都运转良好。我仍然收到信息消息

ConsoleApplication1.vshost.exe Information: 0 : SAPI does not implement phonetic alphabet selection.

......但似乎一切正常。