Windows 在后台使用 SpeechRecognizer 和 UWP 应用

Windows Use SpeechRecognizer with UWP app in background

我需要在用户使用计算机时捕获语音并将其转换为文本。 我知道 UWP 中的 "SpeechRecognizer" class,我已经设置了一个应用程序,如果它在 "Active" 中就可以工作,但是如果你切换应用程序(即使用浏览器),它不会捕获语音。

如何在用户使用其电脑时捕获语音?

尝试 extended execution 请求 OS 在您的应用程序切换到后台后推迟暂停。在我的测试中,SpeechRecognizer 在扩展执行会话中运行良好。

延长执行的时长取决于您使用的是桌面设备还是移动设备,以及设备是连接到墙上电源还是电池电源等因素。

If the device is connected to wall power, there is no limit to the length of the extended execution time period. (but system can revoke a session at any time, the doc has more detailed explanations)

请求延长执行会话的代码很简单,只有下面几行。请记住,您还需要妥善处理扩展执行会话的处置和撤销。

var session = new ExtendedExecutionSession();
session.Reason = ExtendedExecutionReason.Unspecified;
ExtendedExecutionResult result = await session.RequestExtensionAsync();
if (result == ExtendedExecutionResult.Allowed)
{
    //do your long running task here
}

Here是更完整的示例,指导您如何请求、处置和处理扩展执行会话的撤销。

解决了创建 WPF 应用程序并使用 SpeechRecognitionEngine (System.Speech) 而不是 UWP 的 SpeechRecognizer