• 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;

            }

        }

  • 相关阅读:
    【嵌入式】arm-linux-gcc/ld/objcopy/objdump参数概述
    【Java】Java复习笔记-第四部分
    【C/C++】C语言复习笔记-17种小算法-解决实际问题
    【Java】Java复习笔记-三大排序算法,堆栈队列,生成无重复的随机数列
    【Java】Java复习笔记-第三部分
    【教程】ubuntu下安装NFS服务器
    【Java】Java复习笔记-第二部分
    【Java】Java复习笔记-第一部分
    【教程】ubuntu下安装samba服务器
    【C/C++】一道试题,深入理解数组和指针
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14223527.html
Copyright © 2020-2023  润新知