[SpeechSynthesizer类接口官方说明]https://docs.microsoft.com/zh-cn/dotnet/api/system.speech.synthesis.speechsynthesizer?view=netframework-4.0

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
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 SpeechSynthesizerDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnTalk_Click(object sender, RoutedEventArgs e)
        {
            SpeechSynthesizer synthesizer = new SpeechSynthesizer();//创建一个类的实例

            var list=synthesizer.GetInstalledVoices();
            var info = synthesizer.Voice;
            synthesizer.SelectVoice("Microsoft Zira Desktop");

            PromptBuilder promptBuilder = new PromptBuilder();//创建PromptBuilder类的实例          
            promptBuilder.AppendText(textBox1.Text);// AppendText是PromptBuilder类的一个方法,它是参数化的,接受字符串值
            synthesizer.SpeakAsync(promptBuilder);// SpeakAsync是语音合成器类的一个方法,参数化了PromptBuilder类的对象
        }
    }
}

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐