0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Recognition;

namespace ConsoleApp2
{
  class Program
  {
     static SpeechRecognitionEngine recEngine = new 
     SpeechRecognitionEngine();
     bool keyHold = false;

     static void Main(string[] args)
     {
        Choices commands = new Choices();
        commands.Add(new string[] { "Current dollar value", "Current euro value" });
        GrammarBuilder gBuilder = new GrammarBuilder();
        gBuilder.Append(commands);
        Grammar grammar = new Grammar(gBuilder);

        recEngine.LoadGrammarAsync(grammar);
        recEngine.SetInputToDefaultAudioDevice();
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
     }

     void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
     {
         switch (e.Result.Text)
         {
            case "Current dollar value":
                Console.WriteLine("10kr");
                break;

            case "Current euro value":
                Console.WriteLine();
                break;
        }
     }
  }
}

The program is just closing on startup, and does not record my voice, I tried with putting a console.readkey(); under this line in the code recEngine.RecognizeAsync(RecognizeMode.Multiple);. It holds the program up and did not close automatically ofc but still the program does not record my incoming voice command.
Why is this?

4

1 回答 1

1

语音识别是事件驱动的。
您需要定义必要的事件处理程序。
SpeechRecognitionEngine.SpeechRecognized Even必须订阅才能接收识别结果。
另请参阅SpeechRecognitionEngine.AudioStateChanged 事件

此外,在开始识别之前必须使用新设置更新引擎:
请参阅SpeechRecognitionEngine.RequestRecognizerUpdate 方法

注意:
我只使用“ dollar ”和“ euro ”作为语法项目作为提示:尽量在你的语法中使用较少的关键词,否则它们会变得非常难以处理。您可以发音整个“当前美元价值”短语,但公认的关键词是“美元”。

using System.Speech.Recognition;

  static void Main(string[] args)
  {
    using (SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine())
    {
       recEngine.SpeechRecognized += 
             new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
       recEngine.AudioStateChanged += 
             new EventHandler<AudioStateChangedEventArgs>(recEngine_AudioStateChange);

       Choices commands = new Choices();
       commands.Add(new string[] { "dollar", "euro" });
       GrammarBuilder gBuilder = new GrammarBuilder();
       gBuilder.Append(commands);
       Grammar grammar = new Grammar(gBuilder);

       recEngine.SetInputToDefaultAudioDevice();
       recEngine.LoadGrammarAsync(grammar);
       recEngine.RequestRecognizerUpdate();

       recEngine.RecognizeAsync(RecognizeMode.Multiple);

       Console.ReadKey();
       recEngine.SpeechRecognized -= recEngine_SpeechRecognized;
       recEngine.AudioStateChanged -= recEngine_AudioStateChange;
    }
  }

  internal static void recEngine_AudioStateChange(object sender, AudioStateChangedEventArgs e)
  {
     Console.WriteLine("Current AudioLevel: {0}", e.AudioState);
  }

  internal static void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  {
     switch (e.Result.Text)
     {
        case "dollar":
           Console.WriteLine("10kr");
           break;
        case "euro":
           Console.WriteLine("A lot more");
           break;
     }
  }

注意 2:
在 Windows 7System.Speech.Recognition上,无法正常工作。
可以使用Microsoft Speech Platform的免费开发工具启用语音识别和 TTS(文本转语音) 。下载并安装 SDK、运行时和所需语言。这是服务器语音识别实现,开发用于支持通过电话线进行语音识别。因此,它具有非常好的噪声原生过滤器和对发音怪癖的良好容忍度。

于 2018-01-09T00:39:00.250 回答