【问题标题】:C# Save text to speech to MP3 fileC# 将文本到语音保存到 MP3 文件
【发布时间】:2013-04-07 22:17:28
【问题描述】:

我想知道是否有办法将文本转语音数据保存为 mp3 或 Wav 文件格式以便以后播放?

SpeechSynthesizer reader = new SpeechSynthesizer();
reader.Rate = (int)-2;
reader.Speak("Hello this is an example expression from the computers TTS engine in C-Sharp);

我正在尝试将其保存在外部,以便稍后播放。做这个的最好方式是什么?

【问题讨论】:

    标签: c# text-to-speech


    【解决方案1】:

    multiple options 等保存到现有流。如果要新建WAV文件,可以使用SetOutputToWaveFile方法。

    reader.SetOutputToWaveFile(@"C:\MyWavFile.wav");
    

    【讨论】:

      【解决方案2】:

      不是我的回答,从How do can I use LAME to encode an wav to an mp3 c#复制粘贴


      .Net 4.0 中最简单的方法:

      使用 Visual Studio Nuget 包管理器控制台:

      Install-Package NAudio.Lame
      

      代码片段:将语音发送到内存流,然后另存为 mp3:

      //reference System.Speech
      using System.Speech.Synthesis; 
      using System.Speech.AudioFormat;
      
      //reference Nuget Package NAudio.Lame
      using NAudio.Wave;
      using NAudio.Lame; 
      
      
      using (SpeechSynthesizer reader = new SpeechSynthesizer()) {
          //set some settings
          reader.Volume = 100;
          reader.Rate = 0; //medium
      
          //save to memory stream
          MemoryStream ms = new MemoryStream();
          reader.SetOutputToWaveStream(ms);
      
          //do speaking
          reader.Speak("This is a test mp3");
      
          //now convert to mp3 using LameEncoder or shell out to audiograbber
          ConvertWavStreamToMp3File(ref ms, "mytest.mp3");
      }
      
      public static void ConvertWavStreamToMp3File(ref MemoryStream ms, string savetofilename) {
          //rewind to beginning of stream
          ms.Seek(0, SeekOrigin.Begin);
      
          using (var retMs = new MemoryStream())
          using (var rdr = new WaveFileReader(ms))
          using (var wtr = new LameMP3FileWriter(savetofilename, rdr.WaveFormat, LAMEPreset.VBR_90)) {
              rdr.CopyTo(wtr);
          }
      }
      

      【讨论】:

      • 我试过你的相同代码,在本地运行完美,但无法在服务器上创建 mp3 文件。你知道我们在服务器上需要什么配置吗?
      • 您需要在服务器上部署一些 dll 才能让 naudio 工作,例如你把 libmp3lame.32.dll 和 libmp3lame.64.dll 和你的 exe 放在同一个文件夹里了吗?
      • 是的,我确实有它们,我在 Bin 文件夹和根目录中也有它们。但是当我在服务器上运行此应用程序时,它会创建一个大小为 2kb 的 mp3 文件,始终独立于您输入的文本非常大并且 mp3 无法播放。我认为由于服务器上的一些问题,它无法正确创建 mp3。
      • 听起来你需要做更多的挖掘,我不知道问题是什么,至少..
      【解决方案3】:

      通常,如果某些东西在开发工作站上运行但在生产服务器上运行,则它是权限问题。两个想法: Lame 会在某处创建临时文件吗?如果是这样,IIS 进程需要那里的写入权限。 写入输出文件时,IIS 进程再次需要写入权限。 ConvertWavStreamToMp3File(ref ms, "mytest.mp3"); "mytest.mp3" 可能需要是完整路径,使用 Server.MapPath()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-23
        • 1970-01-01
        • 1970-01-01
        • 2019-11-17
        相关资源
        最近更新 更多