wpf Windows 10 语音识别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31902274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows 10 Speech Recognition
提问by pianka
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.
我想在 c# 中为 windows 10 创建一个 WPF 应用程序。现在,我在以前的 windows 版本中遇到的问题是我是意大利语,并且不支持意大利语的语音识别。但现在有 Cortana。那么,如何在我的应用程序中使用 cortana 的语音识别引擎?如果我只是使用new SpeechRecognitionEngine(new CultureInfo("it-IT")));它会给我一个错误,因为没有简单的识别引擎,所以我必须使用 cortana 的引擎。希望您能理解并为我的英语不好而感到抱歉。谢谢您的回答。
回答by Andrew Pilley
In order to use the new SpeechRecognitionWinRT 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.)
为了使用Windows 10 中发布的新SpeechRecognitionWinRT API,您需要向桌面 C# 应用程序添加对 WinRT API 的支持。这不需要将应用程序转换为 Windows 应用商店应用程序,但是,至少在某些部分是这样。据我所知,新引擎尚未向后移植以向 System.Speech.SpeechRecognitionEngine 添加支持,该引擎仍使用旧版识别器(我将在此处与语音团队联系,如果我发现更多信息,请在此帖子中跟进在这一点上。)
Based on the guidance taken from hereand here, I was able to create a classic c# WPF app, and implement the following code:
根据此处和此处的指导,我能够创建一个经典的 c# WPF 应用程序,并实现以下代码:
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
到 .csproj,并从 Project -> Add References -> Universal Windows -> Core 部分添加了 Windows.Media 和 Windows.Foundation,我还手动添加了对
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)
您需要检查 SpeechRecognizer.SupportedGrammarLanguages 以检索 it-IT Language 对象以将其传递给 Recognizer 构造函数,如果您的系统尚未默认为 it-IT。(如果您安装了意大利语版本的 Windows 10,默认情况下应该会发生这种情况)
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.
现在,我上面的代码片段只编译了一个超级简单的语法,它没有开始识别。为此,您需要查阅 Windows.Media.SpeechRecognition API 的其余部分,但大致相同。

