Windows 10 Speech Recognition

前端 未结 1 1421
感情败类
感情败类 2021-02-06 04:56

I want to create a WPF application in c# for windows 10. Now, the problem that i had with previous windows versions was that i\'m italian and there isn\'t a support for speech r

相关标签:
1条回答
  • 2021-02-06 05:08

    In order to use the new SpeechRecognition WinRT API released in windows 10, you're going to need to add support for WinRT APIs to your desktop C# application. This doesn't require converting the app to a Windows Store app, however, at least, for some parts. So far as I know, the new engine hasn't been backported to add support into System.Speech.SpeechRecognitionEngine, that still uses a legacy recognizer (I'll check with the speech team here and follow up in this post if I find more on that point.)

    Based on the guidance taken from here and here, I was able to create a classic c# WPF app, and implement the following code:

    private SpeechRecognizer reco;
    
        public MainWindow()
        {
            InitializeComponent();
    
            reco = new SpeechRecognizer();
            List<string> constraints = new List<string>();
            constraints.Add("Yes");
            constraints.Add("No");
            reco.Constraints.Add(new SpeechRecognitionListConstraint(constraints));
            IAsyncOperation<SpeechRecognitionCompilationResult> op = reco.CompileConstraintsAsync();
            op.Completed += HandleCompilationCompleted;
        }
    
        public void HandleCompilationCompleted(IAsyncOperation<SpeechRecognitionCompilationResult> opInfo, AsyncStatus status)
        {
            if(status == AsyncStatus.Completed)
            {
                System.Diagnostics.Debug.WriteLine("CompilationCompleted");
                var result = opInfo.GetResults();
                System.Diagnostics.Debug.WriteLine(result.Status.ToString());
            }
        }
    

    In order to get this to compile, I added

      <PropertyGroup>
        <TargetPlatformVersion>10.0</TargetPlatformVersion>
      </PropertyGroup>
    

    to the .csproj, and added Windows.Media and Windows.Foundation from the Project -> Add References -> Universal Windows -> Core section, and I also manually added references to

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll
    

    and

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.Runtime.InteropServices.WindowsRuntime.dll
    

    via the browse section of Add References.

    You'll need to check the SpeechRecognizer.SupportedGrammarLanguages to retrieve the it-IT Language object to pass it to the Recognizer constructor, if your system isn't defaulting to it-IT already. (IF you installed an Italian version of windows 10, this should happen by default)

    Now, my code snippet above only compiles a super simple grammar, it doesn't start recognition. You'll need to consult the rest of the Windows.Media.SpeechRecognition API for that, but it's along the same lines.

    0 讨论(0)
提交回复
热议问题