C# System.Speech语音播报朗读字符串

先在引用里添加 System.Speech。
在这里插入图片描述

添加using语句

using System.Speech.Synthesis;

然后就是主程序了。

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SpeechSynthesizer speech = new SpeechSynthesizer();
            speech.Rate = 1;//设置语速
            speech.Volume = 100;//设置音量

            获取本机上所安装的所有的Voice的名称
            //string voicestring = "";
            //foreach (InstalledVoice iv in speech.GetInstalledVoices())
            //{
            //    voicestring += iv.VoiceInfo.Name + ",";
            //}
            //MessageBox.Show(voicestring);

            speech.SelectVoice("Microsoft Huihui Desktop");//设置播音员(中文)
            //speech.SpeakAsync("蒙多说你是个大娘们!");//异步播放
            speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件

        }


        void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            MessageBox.Show("播放完成");
        }
    }

播音员在不同的电脑上可能会预装不同的播音员,如果播音员字符串错误,会出现如下异常:

System.ArgumentException:“不能设置语音。未安装匹配的语音,或语音被禁用。”

在这里插入图片描述
这时候就可以检索PC上安装的语音包来设置对应的语音了。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值