欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

用Microsoft Speech制成的朗读文本小程序(C#)

介绍 评论 失效链接反馈

通过 Windows 自带的 Speech 来朗读文本
from clipboard /* 
 ************************************************************************
 * 程序信息:                                                           *
 *     更改/修改时间:2020年7月24日19:37:16                             *
 *     作者:gfdgd xi                                                   *
 *     调试平台:Visual Studio 2019 Professional 以及 Windows 8.1       *
 *     程序目的:通过 Windows 自带的 Speech 来朗读文本                  *
 ************************************************************************
 */
// 导入头文件
using System;
using System.Windows.Forms;
using System.Speech.Synthesis; // 重点是这个头文件!要加入“System.Speech”的引用
// using System.Collections.Generic;
// using System.ComponentModel;
// using System.Data;
// using System.Drawing;
// using System.Linq;
// using System.Text;
// 这些头文件和程序没什么关系,所以就注释掉了

namespace 语言转文本 // 命名空间
{
    public partial class Form1 : Form
    {
        public Form1() // 实例化程序
        {
            InitializeComponent();
        }
        /// <summary>
        /// 用Microsoft Speech制成的类
        /// </summary>
        public class Speech
        {
            SpeechSynthesizer speech = new SpeechSynthesizer();
            /// <summary>
            /// 文字转语言
            /// </summary>
            /// <param name="say">要朗读的文本</param>
            /// <param name="volume">音量(0~100)</param>
            /// <param name="yusu">语速(-10~10)</param>
            public void Say(string say = "文字转语言", int volume = 100, int yusu = 0)
            {
                speech.Volume = volume; // 设置音量
                speech.Rate = yusu;     // 设置语速
                speech.SetOutputToDefaultAudioDevice();
                speech.Speak(say);      // 朗读文本
                speech.Dispose();       // 释放资源
            }
            /// <summary>
            /// 将朗读文本转为WAV文件
            /// </summary>
            /// <param name="path">保存位置</param>
            /// <param name="saythings">要朗读的文本</param>
            /// <param name="volume">音量(0~100)</param>
            /// <param name="yusu">语速(-10~10)</param>
            public void Save(string path, string saythings = "文字转语言", int volume = 100, int yusu=0)
            {
                speech.SetOutputToWaveFile(path);        // 保存位置
                speech.Rate = yusu;                      // 设置语速
                speech.Volume = volume;                  // 设置音量
                speech.Speak(saythings);            // 读文本
                speech.SetOutputToDefaultAudioDevice();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Speech speech = new Speech();
            speech.Say(textBox1.Text, trackBar1.Value, trackBar2.Value); // 播放音频
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(saveFileDialog1.ShowDialog()
                ==
                DialogResult.OK)
                // 如果用户在另存为对话框点击了“确定”
            {
                Speech speech = new Speech();
                speech.Save(saveFileDialog1.FileName, textBox1.Text, trackBar1.Value, trackBar2.Value); // 另存为音频
                MessageBox.Show("保存完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); // 完成后提示结果
            }
        }
    }
}

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复