0

I'd like to use Windows.Media.SpeechRecognition.SpeechRecognizer in a normal .NET or .NET framework (console) desktop project. Is this possible at all? Here's what I tried so far without success:

  1. Using UwpDesktop to get the above assembly recognized.
  2. Creating a Windows Runtime Component VS project that exposes an interface to SpeechRecognizer. However, trying to use it in a .NET project results in System.PlatformNotSupportedException.

What other ways are there? If none, is there a conventional IPC method to make UWP and normal .NET apps talk on Windows?

w128
  • 4,680
  • 7
  • 42
  • 65

2 Answers2

0

If you want to use Windows.Media.SpeechRecognition.SpeechRecognizer in .NET project, you can add Windows.winmd and System.Runtime.WindowsRuntime.dll in the project References.

Here is my file location for your reference.

C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.16299.0\Windows.winmd

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


Update

My console code sample

enter image description here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Media.SpeechRecognition;

namespace ConsoleApp1
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var recognizer = new SpeechRecognizer();
            await recognizer.CompileConstraintsAsync();
            recognizer.Timeouts.InitialSilenceTimeout = TimeSpan.FromSeconds(5);
            recognizer.Timeouts.EndSilenceTimeout = TimeSpan.FromSeconds(20);
            recognizer.UIOptions.AudiblePrompt = "Say whatever you like, I'm listening";
            recognizer.UIOptions.ExampleText = "This a sample Test";
            recognizer.UIOptions.ShowConfirmation = true;
            recognizer.UIOptions.IsReadBackEnabled = true;
            recognizer.Timeouts.BabbleTimeout = TimeSpan.FromSeconds(5);
            var result = await recognizer.RecognizeWithUIAsync();
            if (result != null)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine($"I have {result.Confidence} confidence that you said [ {result.Text}] " + $"and it took {result.PhraseDuration.TotalSeconds} seconds to say it " + $"starting at {result.PhraseStartTime:g}");
                var alternates = result.GetAlternates(10);
                builder.AppendLine($"There were {alternates?.Count} alternates - listed below (if any)");
                if (alternates != null)
                {
                    foreach (var alternate in alternates)
                    {
                        builder.AppendLine($"Alternate {alternate.Confidence} confident you said [ {alternate.Text}]");
                    }
                }
               
            }
        }
    }
}
Junjie Zhu - MSFT
  • 2,086
  • 1
  • 2
  • 6
  • Thanks, but I already tried that without success, and I read that adding UwpDesktop is supposed to be an alternative. Moreover, I cannot find System.Runtime.WindowsRuntime.dll at the specified location (only a bunch of xml files). Instead, I have it inside Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\system.runtime.windowsruntime\\ref\netcore50. I'm not sure if it's relevant. – w128 Jul 28 '23 at 09:05
  • " I already tried that without success" , Is there any error message? – Junjie Zhu - MSFT Jul 28 '23 at 09:16
  • `The type forwarder for type 'Windows.Foundation.IAsyncOperation1' in assembly 'Windows' causes a cycle` when attempting to call any *Async(). But at least now I can get the namespace to be recognized, which is a progress: I had to add 1) C:\Program Files (x86)\Windows Kits\10\References\...\.winmd 2) System.Runtime.WindowsRuntime (via NuGet) 3) Windows.winmd. – w128 Jul 28 '23 at 12:29
  • Ok, I got it to work like suggested here: https://stackoverflow.com/a/70041524/885844 1. To a .NET framework porject, add NuGet package `Microsoft.Windows.SDK.Contracts` 2. Change to package reference format. However, this only works in non-admin mode, which is weird. As admin, SpeechRecognizer ctor crashes with "Internal speech error" at runtime. – w128 Jul 28 '23 at 13:12
  • I can run it in administrator mode, you can refer to my quick test code. Maybe you can use a file search tool to retrieve the `Windows.winmd` and `System.Runtime.WindowsRuntime.dll`. – Junjie Zhu - MSFT Jul 31 '23 at 06:03
0

Turns out adding Microsoft.Windows.SDK.Contracts NuGet package was what was needed to make it work in a regular .NET console application. You also need to use package reference format.

There are still some issues, though, like it failing to initialize when run in admin mode, and permanently stopping to listen to voice once the window goes out of focus. But I'd consider these as separate issue.

w128
  • 4,680
  • 7
  • 42
  • 65