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

  • 相关阅读:
    P1265 公路修建 最小生成树
    P1991 无线通讯网 最小生成树
    Stock Chase 拓扑
    Rank of Tetris 拓扑排序+并查集
    P1169 [ZJOI2007]棋盘制作 DP悬线法
    P4147 玉蟾宫 二维DP 悬线法
    P1341 无序字母对 欧拉回路
    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs
    BSTestRunner插件的用法.py
    Selenium
  • 原文地址:https://www.cnblogs.com/chengeng/p/5809377.html
Copyright © 2011-2022 走看看