ASP.NET Core 2.0文本转MP3音频:替代System.Speech方案求助
Hey there, I’ve been in your shoes before—losing access to System.Speech when moving to .NET Core was a frustrating roadblock for text-to-speech (TTS) tasks. Let me break down the most reliable alternatives that work seamlessly with ASP.NET Core 2.0:
1. Azure Cognitive Services Speech SDK (推荐,跨平台、稳定)
This is the most robust option for production use, as it’s fully supported for .NET Core and handles MP3 generation natively. Here’s how to set it up:
- Install the NuGet package:
Install-Package Microsoft.CognitiveServices.Speech - Sample code in an ASP.NET Core controller:
Note: You’ll need an Azure account and a Speech resource to get the key/region—this offers a free tier for testing.using Microsoft.CognitiveServices.Speech; using Microsoft.CognitiveServices.Speech.Audio; using Microsoft.AspNetCore.Mvc; using System.IO; using System.Threading.Tasks; [ApiController] [Route("api/tts")] public class TextToSpeechController : ControllerBase { [HttpPost] public async Task<IActionResult> GenerateMp3([FromBody] string text) { // Replace with your Azure Speech resource key and region var config = SpeechConfig.FromSubscription("YOUR_SPEECH_KEY", "YOUR_REGION"); config.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3); using var audioOutput = AudioConfig.FromWavFileOutput(Path.GetTempFileName() + ".mp3"); using var synthesizer = new SpeechSynthesizer(config, audioOutput); var result = await synthesizer.SpeakTextAsync(text); if (result.Reason == ResultReason.SynthesizingAudioCompleted) { var mp3Bytes = System.IO.File.ReadAllBytes(audioOutput.OutputFilePath); System.IO.File.Delete(audioOutput.OutputFilePath); // Clean up temp file return File(mp3Bytes, "audio/mpeg", "output.mp3"); } else { return BadRequest($"TTS failed: {result.Reason}"); } } }
2. NAudio + eSpeak (开源免费,本地运行)
If you don’t want to rely on cloud services, use NAudio with the open-source eSpeak TTS engine. It runs locally but requires a bit more setup:
- Install NuGet packages:
Install-Package NAudio Install-Package NAudio.Vorbis - Setup steps:
- Download the eSpeak binaries from its official repo and extract them to your project’s
wwwrootor a dedicated folder. - Use NAudio to interface with eSpeak and convert its output to MP3:
Keep in mind: eSpeak’s voice quality is more basic compared to cloud services, but it’s great for offline scenarios.using NAudio.Wave; using System.Diagnostics; using System.IO; public byte[] GenerateMp3Local(string text) { var tempWavPath = Path.GetTempFileName() + ".wav"; var tempMp3Path = Path.GetTempFileName() + ".mp3"; // Call eSpeak to generate WAV file var process = new ProcessStartInfo { FileName = "path/to/espeak.exe", Arguments = $"\"{text}\" -w {tempWavPath}", UseShellExecute = false, CreateNoWindow = true }; Process.Start(process).WaitForExit(); // Convert WAV to MP3 using NAudio using var reader = new WaveFileReader(tempWavPath); using var writer = new LameMP3FileWriter(tempMp3Path, reader.WaveFormat, 128); reader.CopyTo(writer); var mp3Bytes = File.ReadAllBytes(tempMp3Path); // Clean up temp files File.Delete(tempWavPath); File.Delete(tempMp3Path); return mp3Bytes; } - Download the eSpeak binaries from its official repo and extract them to your project’s
Why ScriptCs didn’t work
ScriptCs is built around .NET Framework components, so it often relies on APIs like System.Speech that aren’t available in .NET Core. It’s not a viable solution for Core-based projects.
内容的提问来源于stack exchange,提问作者Vikram V

