如何修复错误代码为 0xe (SPXERR_MIC_NOT_AVAILABLE) 的异常

问题描述 投票:0回答:1

我已经构建了一个聊天机器人框架,现在正在寻求为该机器人集成语音功能。我正在尝试使用 python 运行来自 ms learn faststart for voice sdk 的以下代码。

import os
import azure.cognitiveservices.speech as speechsdk
# Explicitly set the values if not already set in the environment
os.environ["SPEECH_KEY"] = "838f0db5598343dcbd8b0d42587501c4"
os.environ["SPEECH_REGION"] = "southeastasia"

SPEECH_KEY = os.getenv("SPEECH_KEY")
SPEECH_REGION = os.getenv("SPEECH_REGION")

def recognize_from_microphone():
    
    # This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
    speech_config = speechsdk.SpeechConfig(subscription=SPEECH_KEY, region=SPEECH_REGION)
    speech_config.speech_recognition_language="en-US"
    print(SPEECH_KEY)
    print(SPEECH_REGION)

    audio_config =speechsdk.audio.AudioConfig(use_default_microphone=True)
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

    print("Speak into your microphone.")
    speech_recognition_result = speech_recognizer.recognize_once_async().get()

    if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
        print("Recognized: {}".format(speech_recognition_result.text))
    elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
        print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
    elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = speech_recognition_result.cancellation_details
        print("Speech Recognition canceled: {}".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            print("Error details: {}".format(cancellation_details.error_details))
            print("Did you set the speech resource key and region values?")


recognize_from_microphone()

我不断收到以下错误。该代码在 Jupyter 笔记本上运行时有效,但在 vscode 中的 powershell 上运行时无效。两者都使用相同的环境和解释器。知道 vscode 上是否有特定权限可以启用麦克风才能工作吗?

Python 版本 - Python 3.11.10

Azure 认知语音版本 - 1.41.1

RuntimeError: Exception with error code:
[CALL STACK BEGIN]

    > CreateModuleObject
    - CreateModuleObject
    - audio_config_get_audio_processing_options
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - audio_config_get_audio_processing_options
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring

[CALL STACK END]

Exception with an error code: 0xe (SPXERR_MIC_NOT_AVAILABLE)

我已经仔细检查了我的默认输入设备,它可以在录音机甚至 jupyter 笔记本上运行,只是在 vscode 上的 powershell 终端上运行时不行。我在 Windows 11 上运行。

python azure visual-studio-code audio-streaming azure-speech
1个回答
0
投票

最初,当我在 vscode 中的 PowerShell 上运行时,我也遇到了同样的错误。

enter image description here

当麦克风不可用或 SDK 无法访问时,Azure 认知语音 SDK 中会出现上述错误。

在 Windows 11 上,首先检查 VS Code 是否可以访问麦克风:

  • 转到设置 > 隐私和安全 > 麦克风

  • 检查是否针对桌面应用程序Microsoft Visual Studio Code专门启用了麦克风访问

enter image description here

然后,在我使用了与您在上面使用的相同的代码之后,效果很好。

VS Code 中的 PowerShell:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.