Windows 10 Speech Recognition

2020-05-27 08:59发布

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 recognition in italian. But now there is cortana. So, how can i use cortana's speech recognition engine for my application? If i simply use new SpeechRecognitionEngine(new CultureInfo("it-IT"))); it gives me an error, 'cause there isn't the simple recongition engine, so i have to use cortana's one. Hope you understood and sorry for my bad english. Thank you for your answer.

1条回答
狗以群分
2楼-- · 2020-05-27 09:27

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.

查看更多
登录 后发表回答