Bài viết

Kho tài liệu và bài viết được chia sẻ, đánh giá bởi cộng đồng

Code SpeechToText

Vo Tan Duc đã tạo 17:27 30-04-2023 181 lượt xem 0 bình luận

Nội dung bài viết

MainWindow.xaml

<Window x:Class="Speech_to_text_WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Speech_to_text_WPF"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <ScrollViewer VerticalScrollBarVisibility="Visible">
            <TextBox x:Name="txtText" AcceptsReturn="True" TextWrapping="Wrap"></TextBox>
        </ScrollViewer>
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition Height="auto"></RowDefinition>
            </Grid.RowDefinitions>
            <Button Content="Start" Click="btnStart_Click" x:Name="btnStart"></Button>
            <Button Grid.Row="1" Click="btnStop_Click" Content="Stop" x:Name="btnStop"></Button>
        </Grid>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Speech_to_text_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        SpeechSynthesizer speechSynthesizer=new SpeechSynthesizer();
        PromptBuilder promptBuilder=new PromptBuilder();
        SpeechRecognitionEngine speechRecognitionEngine=new SpeechRecognitionEngine();
        Choices choices = new Choices();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            btnStart.IsEnabled = false;
            btnStop.IsEnabled = true;
            Grammar grammar=new DictationGrammar();
            try
            {
                speechRecognitionEngine.RequestRecognizerUpdate();
                speechRecognitionEngine.LoadGrammar(grammar);
                speechRecognitionEngine.SpeechRecognized += SpeechRecognitionEngine_SpeechRecognized;
                speechRecognitionEngine.SetInputToDefaultAudioDevice();
                speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"Error");
            }
        }

        private void SpeechRecognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            txtText.Text=txtText.Text+e.Result.Text.ToString()+Environment.NewLine;
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            speechRecognitionEngine.RecognizeAsyncStop();
            btnStart.IsEnabled=true;
            btnStop.IsEnabled=false;
        }
    }
}

 

Nội dung bài viết

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập

Câu hỏi mới nhất