其他分享
首页 > 其他分享> > WPF文本语音播报

WPF文本语音播报

作者:互联网

WPF语音播报案例。

引用:System.Speech

xaml代码

<Window x:Class="Text_Speech.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:Text_Speech"
        mc:Ignorable="d"
        Title="MainWindow" Height="800" Width="300">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListBox Name="listBox" ItemsSource="{Binding TxtContent}"/>
        <Button Margin="5" Grid.Row="1" Content="Load" Click="CmdLoadText_Click"/>
        <Button Margin="5" Grid.Row="2" Content="Speak" Click="CmdSpeak_Click"/>
        <Button Margin="5" Grid.Row="3" Content="PromptTest" Click="CmdPromptTest_Click"/>
        <Button Margin="5" Grid.Row="4" Content="Pause/Resume" Click="CmdPauseResume_Click"/>
    </Grid>
</Window>

后台代码

using System.Windows;
using System.Speech.Synthesis;
using System.IO;
using Microsoft.Win32;
using System.Collections.ObjectModel;

namespace Text_Speech
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<string> TxtContent { get; set; }
        private SpeechSynthesizer synthesizer = new SpeechSynthesizer();

        public MainWindow()
        {
            InitializeComponent();

            TxtContent = new ObservableCollection<string>
            {
                "My dear Little ShuYu"
            };
            listBox.ItemsSource = TxtContent;
        }

        private void CmdLoadText_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog
            {
                Filter = "文本|*.txt",
                RestoreDirectory = true,
                Title = "选择文件"
            };
            string filePath;
            if (openDialog.ShowDialog() == true)
            {
                filePath = openDialog.FileName;
            }
            else
            {
                return;
            }
            TxtContent.Clear();
            var file = File.Open(filePath, FileMode.Open);
            using (StreamReader reader = new StreamReader(file))
            {
                while (!reader.EndOfStream)
                {
                    TxtContent.Add(reader.ReadLine());
                }
            }
            file.Close();
            listBox.ItemsSource = TxtContent;
        }

        private void CmdSpeak_Click(object sender, RoutedEventArgs e)
        {
            SpeechSynthesizer synthesizer = new SpeechSynthesizer();
            synthesizer.SpeakAsync(TxtContent[0]);
        }

        private void CmdPromptTest_Click(object sender, RoutedEventArgs e)
        {
            PromptBuilder prompt = new PromptBuilder();
            PromptStyle style = new PromptStyle
            {
                Rate = PromptRate.Fast,
            };
            prompt.StartStyle(style);
            for (int i = 0; i < TxtContent.Count; i++)
            {
                prompt.AppendText(TxtContent[i]);
            }   
            prompt.EndStyle();
            if (synthesizer.State == SynthesizerState.Paused || synthesizer.State == SynthesizerState.Ready)
            {
                synthesizer = new SpeechSynthesizer();
                synthesizer.SpeakAsync(prompt);
            }
        }

        private void CmdPauseResume_Click(object sender, RoutedEventArgs e)
        {
            if (synthesizer.State == SynthesizerState.Paused)
            {
                synthesizer.Resume();
            }
            else
            {
                synthesizer.Pause();
            }
        }
    }
}

 

标签:播报,TxtContent,System,private,synthesizer,using,new,WPF,文本
来源: https://blog.csdn.net/u012366767/article/details/95611006