0

在我的UWP應用程序中,默認的聽寫語法不能識別語音。但是,當我使用編程列表約束時,它是完全可以識別的。下面是我的代碼的語音識別部分供參考。如果我不評論第五行,這工作正常。我在做下面的錯誤:在我的UWP應用程序中,默認的聽寫語法不能識別語音。

  speechRecognizer = new SpeechRecognizer(); 
      bool PermissionGained = await CheckMicrophonePermission(); 
      if (PermissionGained) 
      { 
       //speechRecognizer.Constraints.Add(new SpeechRecognitionListConstraint(Grammar.GrammarCommands.GrammarConstraintList)); 
       await speechRecognizer.CompileConstraintsAsync(); 

       //recognize speech input at any point of time 
       speechRecognizer.ContinuousRecognitionSession.ResultGenerated += 
        async (s, e1) => 
        { 
         if ((e1.Result != null)) 
         { 
          await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
           async() => 
           { 
            await ParsespeechCommand(e1.Result); 

           }); 
          speechRecognizer.ContinuousRecognitionSession.Resume(); 
         } 
        }; 
       await speechRecognizer.ContinuousRecognitionSession.StartAsync(SpeechContinuousRecognitionMode.PauseOnRecognition); 
      } 
+0

我從你的代碼做了一個演示(用列表代替Grammar.GrammarCommands.GrammarConstraintList),一切正常。你可以設置斷點並檢查Grammar.GrammarCommands.GrammarConstraintList是否正確得到你的應用程序? –

+0

對不起,我無法正確解釋我的問題。這段代碼與列表約束完美匹配,因爲您也經歷過,但我不想使用列表約束,我想使用默認的聽寫語法,以便識別任何自由的語音。所以,在上面的代碼中,我不想使用評論的第5行,這意味着使用默認的聽寫語法。我清楚了嗎? –

+0

我明白了。只是嘗試不使用約束。我不是以英語爲母語的人。有時它也不認識。但大部分時間都可以工作。同樣使用默認的聽寫語法並不意味着完全可以識別自由語音。請參閱[預定義語法](https://msdn.microsoft.com/en-us/windows/uwp/input-and-devices/speech-interactions#Constraints)。 –

回答

0

正如我討論過的,我做了一個演示,並對代碼做了一些更改。 這裏是我的代碼:

private async void myBtn_Click(object sender, RoutedEventArgs e) 
{ 
     //here I remove the checkPermission process 
     var speechRecognizer = new SpeechRecognizer(); 
     if (true) 
     { 
      //speechRecognizer.Constraints.Add(new SpeechRecognitionListConstraint(new List<string> { "winffee", "Elvis", "noob" })); 
      await speechRecognizer.CompileConstraintsAsync(); 

      //recognize speech input at any point of time 
      speechRecognizer.ContinuousRecognitionSession.ResultGenerated += 
       async (s, e1) => 
       { 
        if ((e1.Result != null)) 
        { 
         await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
          () =>//here I remove the async 
          { 
           var result = e1.Result;//here I remove the method to focus on the e1.Result. 
          }); 
         speechRecognizer.ContinuousRecognitionSession.Resume(); 
        } 
       }; 
      await speechRecognizer.ContinuousRecognitionSession.StartAsync(SpeechContinuousRecognitionMode.PauseOnRecognition); 
     } 
} 

整個功能是通過一個按鈕單擊事件觸發。我對每個變化的位置發表了評論(共有3個地方)。

+0

好了Elvis感謝發佈,但沒有任何的變化似乎幫助太多..我需要等待ParseSpeechCommand,所以我把異步...如果我移動「等待speechRecognizer.ContinuousRecognitionSession.StartAsync();」在continuousRecognitionSession.ResultGenerated之前,它有時候開始聆聽...不是大部分時間... –

+0

對於遲到的回覆很抱歉。我試着在lamda表達式中添加一個異步調用。但結果是一樣的。我建議你用「await speechRecognizer.RecognizeWithUIAsync();」來測試語音輸入,它將給出一個UI來顯示結果。 –

+0

當我刪除SpeechContinuousRecognitionMode.PauseOnRecognition,語音被識別,但會話不恢復....意味着它識別語音...相應的命令執行,但然後停止識別。之後,它不承認任何東西謝謝我知道如何獲得UI的東西,但不需要在這個應用程序... –