可以在.NET项目中使用UWP的Windows.Media.SpeechRecognition吗?

huangapple go评论72阅读模式
英文:

Is it possible to use UWP's Windows.Media.SpeechRecognition in a .NET project?

问题

我想在普通的.NET或.NET框架(控制台)桌面项目中使用Windows.Media.SpeechRecognition.SpeechRecognizer。这是否有可能?以下是我尝试但未成功的方法:

  1. 使用UwpDesktop来使上述程序集被识别。
  2. 创建一个Windows Runtime组件VS项目,公开一个SpeechRecognizer接口。然而,在.NET项目中尝试使用它会导致System.PlatformNotSupportedException

还有其他方法吗?如果没有,是否有一种常规的IPC方法可以让UWP和普通的.NET应用程序在Windows上通信?

英文:

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?

答案1

得分: 0

如果您想在.NET项目中使用 Windows.Media.SpeechRecognition.SpeechRecognizer,可以在项目引用中添加 Windows.winmdSystem.Runtime.WindowsRuntime.dll

以下是供您参考的文件位置:

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\System.Runtime.WindowsRuntime.dll

更新

我的控制台代码示例:

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 is 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}]");
                    }
                }
            }
        }
    }
}

<details>
<summary>英文:</summary>

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\UnionMetadata.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][1]][1]

    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 = &quot;Say whatever you like, I&#39;m listening&quot;;
                recognizer.UIOptions.ExampleText = &quot;This a sample Test&quot;;
                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($&quot;I have {result.Confidence} confidence that you said [ {result.Text}] &quot; + $&quot;and it took {result.PhraseDuration.TotalSeconds} seconds to say it &quot; + $&quot;starting at {result.PhraseStartTime:g}&quot;);
                    var alternates = result.GetAlternates(10);
                    builder.AppendLine($&quot;There were {alternates?.Count} alternates - listed below (if any)&quot;);
                    if (alternates != null)
                    {
                        foreach (var alternate in alternates)
                        {
                            builder.AppendLine($&quot;Alternate {alternate.Confidence} confident you said [ {alternate.Text}]&quot;);
                        }
                    }
                   
                }
            }
        }
    }


  [1]: https://i.stack.imgur.com/dPcqZ.png

</details>



# 答案2
**得分**: 0

添加 `Microsoft.Windows.SDK.Contracts` NuGet 包是使其在常规的 .NET 控制台应用程序中正常工作所需的。您还需要使用包引用格式。

但仍然存在一些问题,例如在以管理员模式运行时初始化失败,以及窗口失去焦点后永久停止监听语音。但我认为这些是单独的问题。

<details>
<summary>英文:</summary>

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&#39;d consider these as separate issue.

</details>



huangapple
  • 本文由 发表于 2023年7月28日 00:52:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定