uwp 之语音识别
xml code
----------------------------------------------
<Page
x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<TextBox Name="txtInput" Height="100" TextWrapping="Wrap"/>
<Button Margin="0,15,0,10" Content="点击这里,开始识别" Click="onClick" HorizontalAlignment="Stretch"/>
<TextBlock Name="tbDisplay" FontSize="16" Margin="3,13,0,0" Foreground="Yellow"/>
</StackPanel>
</Page>
C# code
-----------------------------------------------------
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.
// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
}
private async void onClick(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
btn.IsEnabled = false;
using (SpeechRecognizer recognizer = new SpeechRecognizer())
{
try
{
// 编译所有语法协定
SpeechRecognitionCompilationResult compilationResult = await recognizer.CompileConstraintsAsync();
if (compilationResult.Status == SpeechRecognitionResultStatus.Success)
{
// 开始识别
SpeechRecognitionResult recogResult = await recognizer.RecognizeAsync();
// 显示识别结果
if (recogResult.Status == SpeechRecognitionResultStatus.Success)
{
tbDisplay.Text = "识别完成。";
txtInput.Text = recogResult.Text;
}
}
}
catch (Exception ex)
{
tbDisplay.Text = "异常:" + ex.Message;
}
}
btn.IsEnabled = true;
}
}
最新文章
- Oracle转移数据表空间存储位置
- linux下JDK1.7安装
- c#socket编程基础
- java.net.MalformedURLException: Illegal character in URL
- ytu 2029: C语言实验——温度转换(水题)
- Error:(12) No resource identifier found for attribute &#39;titles&#39; in package &#39;com.itheima52.mobilesafe5
- Codeforce 218 div2
- FreeRTOS中断优先级配置(重要)
- mongodb集群【】
- 课后练习:C语言实现Linux命令——od
- linux下面的打包压缩命令
- Entity Framework (EF) Core工具创建一对多和多对多的关系
- spring boot常见问题
- Linux内核总结博客 20135332武西垚
- 把旧系统迁移到.Net Core 2.0 日记 (12) --发布遇到的问题
- 5 -- Hibernate的基本用法 --1 2 基本映射方式
- C/C++中的常量到底存在了什么地方
- C语言:奇偶归一猜想
- 解决sqlplus: command not found
- POJ 1755 Triathlon (半平面交)