zoukankan      html  css  js  c++  java
  • 一个文本转语音的例子

    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自行缩进!

  • 相关阅读:
    ansible入门七(实战)
    ansible入门六(roles)
    ansible入门五
    ansible入门四(Ansible playbook基础组件介绍)
    ansible入门三(Ansible的基础元素和YAML介绍)
    ansible入门二(Ansible常见模块介绍)
    关于XMLHttpRequest对象的responseText属性
    使用WebStorm/Phpstorm实现remote host远程开发
    pageX、clientX、screenX、offsetX、layerX、x
    jQuery.innerWidth() 函数详解
  • 原文地址:https://www.cnblogs.com/chengeng/p/5809377.html
Copyright © 2011-2022 走看看