欢迎光临散文网 会员登陆 & 注册

详解VC#如何实现制作-文字转语音TTS功能-播音程序小工具by大尽长虹

2020-05-04 19:43 作者:大尽长虹  | 我要投稿

准备的工具1:64bitWIN10系统的电脑,vs2010.iso,VsSDK_sfx.exe,

准备的工具2:材料msttss22L.exe,SpeechSDK51LangPack,SpeechSDK51

事先装备好的操作,先安装SpeechSDK51,再安装SpeechSDK51LangPack,再展开msttss22L.exe。电脑上装VS2010.iso然后选中VC#和VC++就足够了。其他功能多余。装完了VS2010以后,最后打上VsSDK_sfx.exe补丁。然后默认的名字新建项目,VC#的窗体运用程序,勾选项目引用管理器的COM下的system.speech。就可以开工了:

如图建立LABEL1,BUTTON1-4,RICHTEXTBOX,TRACKBAR1-2

在FORM1.CS下输入一下完整代码,即可完成这个播音小程序的制作。简单吧。当初可花了我不少心血。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Globalization;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private SpeechSynthesizer speech;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)   //界面初始化
        {
            label1.Text = "请输入你要转换为语音的文字:\n——(中英文均可):";
            String str = "我有一个梦想";
            richTextBox1.Text = str;
            speech = new SpeechSynthesizer();
            button1.Text = "开始";
            button2.Text = "暂停";
            speech.Rate = trackBar1.TabIndex;
            speech.Volume = trackBar2.TabIndex;
        }
        
        private void button1_Click(object sender, EventArgs e)//开始朗读按钮
        {
            speech.Speak(this.richTextBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)//暂停和继续按钮
        {
            if (button2.Text == "暂停")
            {
                speech.Pause();
                button2.Text = "继续";
            }
            else
            {
                speech.Resume();
                button2.Text = "暂停";
            }
        }

        private void button3_Click(object sender, EventArgs e)//保存为音频文件
        {
            SpeechSynthesizer speechSyn = new SpeechSynthesizer();
            speechSyn.Volume = trackBar2.TabIndex;
            speechSyn.Rate = trackBar1.TabIndex;
            speechSyn.SetOutputToWaveFile("D:\\Record.wav");
            speechSyn.Speak(richTextBox1.Text);
            speechSyn.SetOutputToDefaultAudioDevice();
            MessageBox.Show("保存录音文件成功,保存路径:D:\\Record.wav!");
            speechSyn.Dispose();
        }

        private void button4_Click(object sender, EventArgs e)//退出应用
        {
            speech.Dispose();
            Application.Exit();
        }
    }
}

保存退出,得到的文件使用管理员身份运行就可以了。太开心了。

详解VC#如何实现制作-文字转语音TTS功能-播音程序小工具by大尽长虹的评论 (共 条)

分享到微博请遵守国家法律