2016-04-15 55 views
0

對於我第一個UWP應用程序項目,我想構建一個帶語音識別的小型測試程序。UnauthorizedAccessException或如何爲UWP應用程序啓用Microfone

通過一些教程Searchin的後,我想出了下面的代碼:

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

我總是未經授權的訪問例外。在Google上閱讀了一些帖子之後,我認爲這可能是因爲應用程序對我的麥克風有些問題。那麼,如何爲我的應用程序啓用麥克風?

回答

相關問題