You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#.NET语音识别应用遇System.UnauthorizedAccessException权限错误求助

Fixing System.UnauthorizedAccessException for SpeechRecognitionEngine in C# WinForms

Hey there, let's work through this System.UnauthorizedAccessException (E_ACCESSDENIED) issue you're hitting with your speech recognition project. I've looked over your error stack trace and code, and here are the most likely fixes to get your app running smoothly:

1. Verify Microphone Permissions on Windows

First and foremost, your app needs explicit permission to access your system's microphone. Here's how to check:

  • Open Windows Settings → Privacy & Security → Microphone
  • Ensure the toggle for "Allow apps to access your microphone" is turned on
  • If you're debugging in Visual Studio, make sure "Allow desktop apps to access your microphone" is also enabled (this covers WinForms apps)

2. Fix Concurrent Audio Device Access in Your Code

Looking at your Form1_Load method, you're initializing two SpeechRecognitionEngine instances (speechRecognitionEngine and speechListener) and both are trying to grab the default audio device at the same time. This is a common cause of access denied errors—two processes can't hold exclusive access to the microphone simultaneously.

You also have redundant code: you're loading the same grammar twice into speechRecognitionEngine, and accidentally loading it into the wrong engine for speechListener. Here's a cleaned-up version of your Form1_Load method to avoid these issues:

private void Form1_Load(object sender, EventArgs e)
{
    // Initialize main recognition engine
    speechRecognitionEngine.SetInputToDefaultAudioDevice();
    var commandGrammar = new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"Commands.txt"))));
    speechRecognitionEngine.LoadGrammarAsync(commandGrammar);
    speechRecognitionEngine.SpeechRecognized += Default_SpeechRecognized;
    speechRecognitionEngine.SpeechRecognized += _recognizer_SpeechRecognized;
    speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

    // Initialize listener engine but don't claim the mic yet
    var listenerGrammar = new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"Commands.txt"))));
    speechListener.LoadGrammarAsync(listenerGrammar);
    speechListener.SpeechRecognized += startlistening_SpeechRecognized;
    // Wait to set input and start listening until the user triggers "Stop listening"
}

Then update your command handlers to safely switch between engines and release the microphone when not in use:

Update "Stop listening" handler:

if(speech == "Stop listening") {
    speechSynthesizer.SpeakAsync("Call me any time you want");
    speechRecognitionEngine.RecognizeAsyncCancel();
    speechRecognitionEngine.SetInputToNull(); // Release the mic from main engine
    speechListener.SetInputToDefaultAudioDevice(); // Claim mic for listener
    speechListener.RecognizeAsync(RecognizeMode.Multiple);
}

Update "Hello assistant" handler:

private void startlistening_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) {
    string speech = e.Result.Text;
    if(speech == "Hello assistant") {
        speechListener.RecognizeAsyncCancel();
        speechListener.SetInputToNull(); // Release mic from listener
        speechSynthesizer.SpeakAsync("Hello, what do you want");
        speechRecognitionEngine.SetInputToDefaultAudioDevice(); // Reclaim mic for main engine
        speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
    }
}

3. Run Your App with Administrator Privileges

Sometimes system-level audio access requires elevated permissions. Try:

  • Right-clicking Visual Studio (or your compiled EXE) and selecting Run as administrator
  • If debugging, you can set this permanently in Visual Studio: Right-click your project → Properties → Debug → Check "Enable native code debugging" (or just run VS as admin each time)

4. Check for Other Apps Using the Microphone

Make sure no other applications (like Zoom, Teams, Discord, or recording software) are currently using your microphone. These apps can lock the device, preventing your C# app from accessing it.


内容的提问来源于stack exchange,提问作者SooryaSRajan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 15:47:32