SpeechSynthesis.axaml代码
<Window xmlns="https://github.com/avaloniaui" 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" Height="300" Width="300" x:Class="AvaloniaUI.SpeechSynthesis" Title="SpeechSynthesis"> <StackPanel Margin="10" Spacing="8"> <TextBox x:Name="txtWords" Watermark="Enter text to speak"/> <Button Content="Speak" Click="CmdSpeak_Click"/> <Button Content="Prompt Test" Click="CmdPromptTest_Click"/> </StackPanel> </Window>
SpeechSynthesis.axaml.cs代码
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using System;
using System.Speech.Synthesis;
namespace AvaloniaUI;
public partial class SpeechSynthesis : Window
{
public SpeechSynthesis()
{
InitializeComponent();
}
private void CmdSpeak_Click(object? sender, RoutedEventArgs e)
{
using var synthesizer = new SpeechSynthesizer();
synthesizer.Speak(txtWords.Text);
}
private void CmdPromptTest_Click(object? sender, RoutedEventArgs e)
{
var prompt = new PromptBuilder();
prompt.AppendText("How are you");
prompt.AppendBreak(TimeSpan.FromSeconds(2));
prompt.AppendText("How ", PromptEmphasis.Reduced);
var style = new PromptStyle
{
Rate = PromptRate.ExtraSlow,
Emphasis = PromptEmphasis.Strong
};
prompt.StartStyle(style);
prompt.AppendText("are ");
prompt.EndStyle();
prompt.AppendText("you?");
using var synthesizer = new SpeechSynthesizer();
synthesizer.Speak(prompt);
}
}
运行效果

浙公网安备 33010602011771号