UnauthorizedAccessException或如何为UWP应用程序启用Microfone

时间:2016-04-15 11:52:54

标签: c# speech-recognition windows-10-universal

对于我的第一个UWP应用程序项目,我想构建一个带语音识别的小型测试程序。

在搜索完一些教程后,我想出了以下代码:

private async void initSpeechParts() {
    try {
        if (_recognizer == null) {
            _recognizer = new SpeechRecognizer();
            var lang= _recognizer.CurrentLanguage;
            var functionGrammar = new SpeechRecognitionTopicConstraint(SpeechRecognitionScenario.Dictation, "Functioncall");
            _recognizer.UIOptions.AudiblePrompt = "Give me the name of a Function";
            _recognizer.UIOptions.ExampleText = "Function A";
            _recognizer.Constraints.Add(functionGrammar) ;
            await _recognizer.CompileConstraintsAsync();
        }

        try {
            // Start recognition.
            SpeechRecognitionResult speechRecognitionResult = await _recognizer.RecognizeWithUIAsync();
            var recognizedText = speechRecognitionResult.Text;
            recognizedText = removetokens(recognizedText);
            callTestfunction(recognizedText);
        } catch (Exception ex2) {
            var ee = ex2.Message;
            throw;
        }                    
    } catch (Exception ex) {
        //Check if user has approved the privacy policy
        const uint HresultPrivacyStatementDeclined = 0x80045509;
        if ((uint)ex.HResult == HresultPrivacyStatementDeclined)
        {
            var messageDialog = new Windows.UI.Popups.MessageDialog(
                "You must accept the speech privacy policy to continue", "Speech Exception");
            messageDialog.ShowAsync().GetResults();
        }
    }
}

我目前对本准则的问题在于它所说的内容:

SpeechRecognitionResult speechRecognitionResult = await _recognizer.RecognizeWithUIAsync();

我总是得到一个UnauthorizedAccess异常。在阅读Google上的一些帖子之后,我开始认为这可能是因为应用程序在我的麦克风上存在一些问题。那么如何为我的应用程序启用麦克风呢?

1 个答案:

答案 0 :(得分:1)

您可能会错过package.appxmanifest中的麦克风功能 只需在Visual Studio中打开清单,然后从功能选项卡中添加microphone功能。

如果需要,您还可以在此处找到演讲样本:https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/SpeechRecognitionAndSynthesis/cpp/Package.appxmanifest