• 一个文本转语音的例子


    1、首先在VS环境下建立一个窗体

    2、然后在网上下载一个DotNetSpeech.dll,并添加引用到项目中。

    3、代码如下:

    3.1 试听部分

    private void btnTestListen_Click(object sender, EventArgs e)
    {
    try
    {
    SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
    SpVoice voice = new SpVoice();
    voice.Volume = 100;
    voice.Rate = Convert.ToInt32(txtSpeed.Text);//朗读语速

    voice.Speak(this.txtContent.Text, spFlags);
    }
    catch (Exception)
    {
    MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }

    3.2 生成mp3文件部分

    private void btnExportMp3_Click(object sender, EventArgs e)
    {
    try
    {
    SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
    SpVoice voice = new SpVoice();
    voice.Volume = 100;
    voice.Rate = Convert.ToInt32(txtSpeed.Text);//朗读语速
    voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);

    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
    sfd.Title = "Save to a wave file";
    sfd.FilterIndex = 2;
    sfd.RestoreDirectory = true;
    if (sfd.ShowDialog() == DialogResult.OK)
    {
    SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
    SpFileStream spFileStream = new SpFileStream();
    spFileStream.Open(sfd.FileName, SpFileMode, false);
    voice.AudioOutputStream = spFileStream;
    voice.Speak(txtContent.Text, spFlags);
    voice.WaitUntilDone(Timeout.Infinite);
    spFileStream.Close();
    }
    }
    catch (Exception)
    {
    MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }

    4、代码无法格式化比较乱,请Copy到VS自行缩进!

  • 相关阅读:
    BE Learing 2 名词解释
    mysql学习笔记(二)之一个粗心的问题
    Struts2/XWork < 2.2.0远程执行任意代码漏洞分析及修补
    DataReceivedEventHandler 委托
    JS数组方法汇总 array数组元素的添加和删除
    jQuery学习总结(一)
    js的lock
    mysql学习笔记(一)之mysqlparameter
    Time Span Attack
    Web Vulnerability Scanner 7.0 Patch for 2010_09_21_01
  • 原文地址:https://www.cnblogs.com/chengeng/p/5809377.html
Copyright © 2020-2023  润新知