Aeglasin
Aeglasin

Reputation: 3

UnauthorizedAccessException or how to Enable Microfone for UWP Application

For my very first UWP application project, I wanted to build a small test Programm with speech recognition.

After searchin through some tutorials I came up with the following Code:

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

My current issue with this Code, is that on the line where it says:

SpeechRecognitionResult speechRecognitionResult = await _recognizer.RecognizeWithUIAsync();

I always get a UnauthorizedAccess Exception. After reading some Posts on Google I came to think that this might be because the application has some Issue with my microphone. So how can I enable the microphone for my application?

Upvotes: 0

Views: 476

Answers (1)

Vincent
Vincent

Reputation: 3746

You probably miss the microphone capability in your package.appxmanifest Just open the manifest in Visual Studio and add the microphone capability from the capabilities tab.

You can also find a sample of speech here if needed : https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/SpeechRecognitionAndSynthesis/cpp/Package.appxmanifest

Upvotes: 1

Related Questions