【发布时间】:2020-06-04 07:26:05
【问题描述】:
我目前正在尝试在我的一个 c# 项目中实现一些语音识别,所以我找到了这个库: https://docs.microsoft.com/en-us/dotnet/api/system.speech.recognition.speechrecognitionengine?view=netframework-4.8
提供此代码作为示例:
using System;
using System.Speech.Recognition;
namespace SpeechRecognitionApp
{
class Program
{
static void Main(string[] args)
{
// Create an in-process speech recognizer for the en-US locale.
using (
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("en-US")))
{
// Create and load a dictation grammar.
recognizer.LoadGrammar(new DictationGrammar());
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Configure input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple);
// Keep the console window open.
while (true)
{
Console.ReadLine();
}
}
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Recognized text: " + e.Result.Text);
}
}
}
这应该完全符合我的需要。 所以我在 Visual Studio 中创建了一个新项目,复制粘贴代码并运行它。
没有编译错误,但是 SpeechRecognitionEngine 的构造函数以非空 CultureInfo 对象为参数,在 System.Speech.dll 中抛出 System.NullReferenceException。
为了尝试调试这个,我确定了
new System.Globalization.CultureInfo("en-US")
返回一个非空对象,并且该区域性已安装。 我还将我的框架更新到 4.8,并以管理员和普通用户的身份运行该项目。 我也在使用 x64 CPU 平台,因为任何 CPU 平台的构建都会失败。
在我看来,我在某处配置错误,因为代码本身不应该是错误的。
你知道如何解决我的问题吗?
感谢您的帮助。
编辑:这可能与这个问题有关,但我不知道它是否有任何帮助: NullReferenceException when starting recognizer with RecognizeAsync
【问题讨论】:
-
我重新打开了你的问题。 @Zohar OP 的代码是链接到的文档示例 OP 的逐字副本。此外,OP 已经进行了充分的事实调查,我们知道这在他们的代码中不是问题。
-
代码本身就是一个很好的副本。但也许还有其他东西要补充。
-
@John,您重新打开它可能是件好事。 c# 问题标题中的 NullReferenceException 使它成为一个非常好的候选人恕我直言,作为一个骗子被关闭,但在仔细阅读问题的内容后,我认为你可能是正确的。
-
我无法使用为调试而构建的 .NET Framework 4.7.2 在 Win 10 x64 Enterprise N(英国英语)上重现此问题。
-
您可以尝试重新启动,然后在您的系统上进行清理/重建并重试吗?
标签: c# speech-recognition nullreferenceexception