【问题标题】:Speech SDK - LoadGrammar() raising UnauthorizedAccessException语音 SDK - LoadGrammar() 引发 UnauthorizedAccessException
【发布时间】:2023-03-20 16:20:01
【问题描述】:

我正在尝试在我的 Windows 8 上使用 C# (4.5) 上的语音识别库。

我安装了“Microsoft Speech Platform SDK 11”,但使用 LoadGrammar 收到异常。

我的程序:

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

namespace SpeechRecognition
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an in-process speech recognizer for the en-US locale.
            using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine())
            {
                // Create and load a dictation grammar.
                // An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Speech.dll

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

System.Speech.dll 中出现“System.UnauthorizedAccessException”类型的未处理异常

堆栈跟踪:

他们 System.Speech.Recognition.RecognizerBase.Initialize(SapiRecognizer 识别器,布尔 inproc) em System.Speech.Recognition.SpeechRecognitionEngine.get_RecoBase() em System.Speech.Recognition.SpeechRecognitionEngine.LoadGrammar(语法 语法) em SpeechRecognition.Program.Main(String[] args) na e:\TestCenter\SpeechRecognition\SpeechRecognition\Program.cs:linha 23 em System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集, 字符串[] args) em Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
em System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 时间 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 时间 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、Object 状态)

我在Win7和Win8下测试过,但是没有一个能用。

有人可以帮我吗?

【问题讨论】:

  • 这不是导致您的问题的代码。不可能从中得到那个错误。
  • 我想你忘了在文件开头使用这两行:using System;using System.Speech.Recognition;
  • @AndrewBarber 确实如此。这是我的代码的第一行。
  • @Oliboy50 实际上我正在使用(使用 System;使用 System.Speech.Recognition;使用 System.Speech.Synthesis;)。 =/
  • @Crasher 这可能是第一行,但不可能是导致该错误的行。

标签: c# windows-8 speech-recognition


【解决方案1】:

奇怪的是,我似乎记得 Speech SDK 有类似的问题,但找不到解决方案。我认为它涉及更改所有者或访问您机器上某些文件或文件夹的权限。也许更多的 googlebinging 可以帮助您找到我当时找到的解决方案,或者您可以使用 ProcessMonitor 来查看该过程正在尝试做什么以及失败。也许 eventvwr 会显示一些东西。

我尝试安装 Speech Platform SDK 11 和 Speech Platform Runtime,但我认为这些可能是在其 .NET 包装器中使用 Microsoft.Speech 命名空间的技术的服务器版本。我还安装了 Speech SDK 5.3,但我认为这不是最新版本。最终我安装了Windows 8.1 SDK,我认为这对我有用。这在我的 WPF 应用程序中运行良好:

XAML:

<Window x:Class="SpeechTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock
            x:Name="tb"/>
    </Grid>
</Window>

C#

using System.Diagnostics;
using System.Globalization;
using System.Speech.Recognition;
using System.Windows;

namespace SpeechTestApp
{
    public partial class MainWindow : Window
    {
        private SpeechRecognitionEngine recognizer;

        public MainWindow()
        {
            InitializeComponent();
            // Create a SpeechRecognitionEngine object for the default recognizer in the en-US locale.
            this.recognizer = new SpeechRecognitionEngine(new CultureInfo("en-US"));
            {

                // Create a grammar for finding services in different cities.
                Choices services = new Choices(new string[] { "restaurants", "hotels", "gas stations" });
                Choices cities = new Choices(new string[] { "Seattle", "Boston", "Dallas" });

                GrammarBuilder findServices = new GrammarBuilder("Find");
                findServices.Append(services);
                findServices.Append("near");
                findServices.Append(cities);

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

                // Add a handler for the speech recognized event.
                recognizer.SpeechRecognized += recognizer_SpeechRecognized;
                recognizer.SpeechDetected += RecognizerOnSpeechDetected;
                recognizer.SpeechHypothesized += RecognizerOnSpeechHypothesized;
                recognizer.SpeechRecognitionRejected += RecognizerOnSpeechRecognitionRejected;
                recognizer.AudioStateChanged += RecognizerOnAudioStateChanged;
                recognizer.AudioSignalProblemOccurred += RecognizerOnAudioSignalProblemOccurred;

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

                // Start asynchronous, continuous speech recognition.
                recognizer.RecognizeAsync(RecognizeMode.Multiple);
            }
        }

        private void RecognizerOnAudioSignalProblemOccurred(object sender, AudioSignalProblemOccurredEventArgs audioSignalProblemOccurredEventArgs)
        {
            Debug.WriteLine(audioSignalProblemOccurredEventArgs.AudioSignalProblem.ToString());
        }

        private void RecognizerOnAudioStateChanged(object sender, AudioStateChangedEventArgs audioStateChangedEventArgs)
        {
            Debug.WriteLine(audioStateChangedEventArgs.AudioState.ToString());
        }

        private void RecognizerOnSpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs speechRecognitionRejectedEventArgs)
        {
            Debug.WriteLine("RecognizerOnSpeechRecognitionRejected: " + speechRecognitionRejectedEventArgs.Result.Text);
        }

        private void RecognizerOnSpeechHypothesized(object sender, SpeechHypothesizedEventArgs speechHypothesizedEventArgs)
        {
            Debug.WriteLine("Hypothesized: " + speechHypothesizedEventArgs.Result.Text);
            tb.Text = speechHypothesizedEventArgs.Result.Text;
        }

        private void RecognizerOnSpeechDetected(object sender, SpeechDetectedEventArgs e)
        {
            Debug.WriteLine("Detected position: " + e.AudioPosition);
        }

        private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Debug.WriteLine("Recognized text: " + e.Result.Text);
            tb.Text = e.Result.Text;
        }
    }
}

【讨论】:

  • 谢谢 Filip... 我真的不知道如何解决这个问题。我猜它没有正确安装oO
  • 运行代码“System.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers()”它会返回 0 个已安装的识别器。听起来它与win8不兼容..
  • 如果您查看 [LoadGrammar()](http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.loadgrammar(v=vs.110).aspx) - they initialize SpeechRecognitionEngine` 的文档,如下所示:new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"))。也许文化信息对于找到识别器至关重要?
  • 如果您的系统语言是普通话,并且语音 SDK 仅提供美式英语和其他一些语言的识别器,但不提供普通话,这似乎非常重要。
  • 我看到如果你安装 SDK11 东西,你可以从这里添加 Microsoft.Speech:"c:\Program Files\Microsoft SDKs\Speech\v11.0\Assembly\Microsoft.Speech.dll"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 2014-05-19
相关资源
最近更新 更多