关键类 SpeechSynthesizer
using System.Speech.Synthesis;
filePath = Path.Combine(filePath, $"{fileName}.wav");
//string filePath = _hostingEnvironment.ContentRootPath + $"\\wwwroot\\speech\\{fileName}.wav";
if (System.IO.File.Exists(filePath))
{
return Json(new { code = 0, msg = "语音存在了!", url = $"/speech/{fileName}.wav" });
}
else
{
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
synth.SetOutputToWaveFile(filePath, new SpeechAudioFormatInfo(32000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
PromptBuilder builder = new PromptBuilder();
builder.AppendText(text);
synth.Speak(builder);
}
return Json(new { code = 2, msg = "生成语音成功!", url = $"/speech/{fileName}.wav" });
}
System.Speech.Synthesis 命名空间