使用语音识别提供输入、指定操作或命令,并在Windows 应用 SDK应用中完成任务。
Important
语音识别需要 MSIX 包标识。 仅当应用在使用包标识的情况下运行时(已打包,或采用外部位置打包),Windows.Media.SpeechRecognition API 才可用。 未打包的应用不能使用这些 API。
关键 API
概述
语音识别包括语音运行时、用于编程运行时的识别 API、用于听写和 Web 搜索的现成语法以及帮助用户发现和使用语音识别功能的默认系统 UI。
配置语音识别
若要支持语音识别,用户必须在其设备上连接并启用麦克风,并接受Microsoft隐私策略授予应用使用它的权限。
在应用的程序包清单中设置 麦克风 设备功能,提示用户获得麦克风访问权限。 有关详细信息,请参阅 应用功能声明。
如果用户授予访问权限,应用会显示在“设置隐私>麦克风”>的已批准应用程序列表中。 由于用户可以随时禁用此设置,因此在尝试使用它之前,请确认你的应用具有麦克风访问权限。
如果还想要支持听写或其他语音识别服务(如主题约束中定义的 预定义语法 ),请确认已启用 联机语音识别 (设置 > 隐私 > 语音)。
以下示例演示如何检查麦克风是否存在,以及你的应用是否有权使用它。
public class AudioCapturePermissions
{
private static int NoCaptureDevicesHResult = -1072845856;
/// <summary>
/// Checks whether the microphone is available and the app has permission to use it.
/// Perform this check every time the app gets focus, because the user can change
/// the setting while the app is suspended or not in focus.
/// </summary>
/// <returns>True if the microphone is available.</returns>
public static async Task<bool> RequestMicrophonePermission()
{
try
{
var settings = new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Audio,
MediaCategory = MediaCategory.Speech
};
var capture = new MediaCapture();
await capture.InitializeAsync(settings);
}
catch (TypeLoadException)
{
// Media player components are not available.
return false;
}
catch (UnauthorizedAccessException)
{
// Permission to use the audio capture device is denied.
return false;
}
catch (Exception exception)
{
if (exception.HResult == NoCaptureDevicesHResult)
{
// No audio capture devices are present on this system.
return false;
}
throw;
}
return true;
}
}
识别语音输入
约束定义应用在语音输入中识别的字词和短语(词汇)。 约束是语音识别的核心,可让你的应用更好地控制识别准确性。
可以使用以下类型的约束。
预定义语法
预定义听写和 Web 搜索语法提供语音识别,而无需编写语法。 使用这些语法时,远程 Web 服务执行识别并将结果返回到设备。
默认的自由文本听写语法可识别用户可以使用特定语言说出的大部分字词和短语,并针对短短语进行优化。 如果未为 SpeechRecognizer 指定任何约束,则使用预定义的听写语法。
Web 搜索语法包含大量字词和短语,针对人们在搜索 Web 时通常使用的字词进行优化。
注释
预定义听写和 Web 搜索语法可能很大,因为它们处于联机状态(不在设备上),因此性能可能不如在本地安装自定义语法的速度一样快。
这些预定义的语法可识别长达 10 秒的语音输入,无需创作。 但是,它们需要网络连接。
请参阅 SpeechRecognitionTopicConstraint。
编程化的列表约束
编程列表约束提供了使用字词或短语列表创建简单语法的轻型方法。 列表约束非常适用于识别短而不同的短语。 指定语法中的所有字词可提高识别准确性,因为语音识别引擎只需要处理语音来确认匹配。 还可以以编程方式更新列表。
请参阅 SpeechRecognitionListConstraint。
SRGS 语法
语音识别语法规范(SRGS)语法是使用 SRGS 版本 1.0 定义的 XML 格式的静态文档。 SRGS 语法允许在单个识别中捕获多个语义含义,从而对语音识别体验进行最大的控制。
请参阅 SpeechRecognitionGrammarFileConstraint。
语音命令约束
使用语音命令定义 (VCD) XML 文件来定义用户在激活应用时可以说出的用于启动操作的命令。 请参阅 SpeechRecognitionVoiceCommandDefinitionConstraint。
若要开始使用约束,请参阅 “定义自定义识别约束”。
基本识别示例
以下示例创建语音识别器,编译默认听写约束,并开始侦听语音输入。
private async void StartRecognizing_Click(object sender, RoutedEventArgs e)
{
var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
// Compile the default dictation grammar.
await speechRecognizer.CompileConstraintsAsync();
// Start recognition.
Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
await speechRecognizer.RecognizeAsync();
// Display the recognized text.
if (result.Status == Windows.Media.SpeechRecognition.SpeechRecognitionResultStatus.Success)
{
resultTextBlock.Text = result.Text;
}
}
注释
此示例使用 RecognizeAsync (没有 UI)。 如果要使用内置识别 UI,请改为调用 RecognizeWithUIAsync 。 请参阅 SpeechRecognizerUIOptions 自定义选项。
自定义识别 UI
当应用调用 SpeechRecognizer.RecognizeWithUIAsync 时,系统将按顺序显示多个屏幕:
- 对于预定义的语法(听写或 Web 搜索): 倾听 → 思考 → 听到你说 (或错误)。
- 对于列表或 SRGS 约束: 侦听 → 你说 (如果模棱两可)→ 听到你说 (或错误)。
使用 SpeechRecognizerUIOptions 类(通过 SpeechRecognizer.UIOptions获取)自定义 侦听 屏幕:
private async void WeatherSearch_Click(object sender, RoutedEventArgs e)
{
var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
speechRecognizer.RecognitionQualityDegrading += SpeechRecognizer_RecognitionQualityDegrading;
var webSearchGrammar = new Windows.Media.SpeechRecognition.SpeechRecognitionTopicConstraint(
Windows.Media.SpeechRecognition.SpeechRecognitionScenario.WebSearch, "webSearch");
speechRecognizer.UIOptions.AudiblePrompt = "Say what you want to search for...";
speechRecognizer.UIOptions.ExampleText = @"Ex. 'weather for London'";
speechRecognizer.Constraints.Add(webSearchGrammar);
await speechRecognizer.CompileConstraintsAsync();
Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
await speechRecognizer.RecognizeWithUIAsync();
resultTextBlock.Text = result.Text;
}