在C#开发过程中,文字转语音是常见的功能需求,不同场景下可以选择不同的实现方案,其中System.Speech和Azure TTS是最常用的两种选择。

方案一:使用System.Speech实现本地文字转语音
System.Speech是.NET Framework内置的语音处理命名空间,基于Windows系统的语音引擎实现,无需额外安装依赖,适合本地运行的桌面应用、控制台应用等场景。
环境准备
首先需要在项目中添加System.Speech的引用,如果你使用的是.NET Framework项目,可以直接在引用管理器中找到并添加。如果是.NET Core或.NET 5及以上版本,需要通过NuGet安装System.Speech.Synthesis包。
基础实现代码
以下是使用System.Speech实现文字转语音的基础示例:
using System;
using System.Speech.Synthesis;
namespace SpeechDemo
{
class Program
{
static void Main(string[] args)
{
// 创建语音合成器实例
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
// 设置语音输出方式,可以选择播放到扬声器或者保存为音频文件
synth.SetOutputToDefaultAudioDevice();
// 要转换的文本内容
string text = "欢迎使用C#文字转语音功能,这是System.Speech方案的演示内容";
// 执行语音合成
synth.Speak(text);
}
}
}
}
高级配置
System.Speech还支持调整语音的音量、语速、选择不同的发音人等配置,示例代码如下:
using System;
using System.Speech.Synthesis;
namespace SpeechDemo
{
class Program
{
static void Main(string[] args)
{
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
// 设置音量,范围0-100
synth.Volume = 80;
// 设置语速,范围-10到10,0为正常语速
synth.Rate = 0;
// 获取并选择可用的发音人
foreach (InstalledVoice voice in synth.GetInstalledVoices())
{
VoiceInfo info = voice.VoiceInfo;
Console.WriteLine($"可用发音人:{info.Name},性别:{info.Gender},年龄:{info.Age}");
}
// 选择指定发音人
synth.SelectVoice("Microsoft Huihui - Chinese (Simplified)");
// 也可以保存为音频文件
synth.SetOutputToWaveFile("output.wav");
string text = "这是保存为音频文件的演示内容";
synth.Speak(text);
}
}
}
}
方案二:使用Azure TTS实现云端文字转语音
Azure TTS是微软Azure认知服务提供的文本转语音服务,支持超过140种语言和变体,提供多种自然度高的神经网络音色,适合需要高质量语音、跨平台运行的应用场景。
环境准备
首先需要前往Azure门户创建语音服务资源,获取订阅密钥和服务区域。然后在项目中通过NuGet安装Microsoft.CognitiveServices.Speech包。
基础实现代码
以下是使用Azure TTS实现文字转语音的基础示例:
using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
namespace AzureTtsDemo
{
class Program
{
static async Task Main(string[] args)
{
// 替换为你的Azure语音服务订阅密钥和服务区域
string subscriptionKey = "your_subscription_key";
string region = "your_region";
// 创建语音配置
var config = SpeechConfig.FromSubscription(subscriptionKey, region);
// 设置语音输出的语言和声线,这里选择中文女声
config.SpeechSynthesisVoiceName = "zh-CN-XiaoxiaoNeural";
// 创建语音合成器,输出到默认音频设备
using (var synthesizer = new SpeechSynthesizer(config, null))
{
string text = "欢迎使用Azure文本转语音服务,这是神经网络音色的演示内容";
// 执行异步语音合成
var result = await synthesizer.SpeakTextAsync(text);
// 检查合成结果
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine("语音合成完成");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"语音合成取消,原因:{cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"错误详情:{cancellation.ErrorDetails}");
}
}
}
}
}
}
保存音频文件示例
如果需要将合成后的语音保存为文件,可以使用以下代码:
using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
namespace AzureTtsDemo
{
class Program
{
static async Task Main(string[] args)
{
string subscriptionKey = "your_subscription_key";
string region = "your_region";
var config = SpeechConfig.FromSubscription(subscriptionKey, region);
config.SpeechSynthesisVoiceName = "zh-CN-YunxiNeural";
// 配置输出到音频文件
using (var audioConfig = AudioConfig.FromWavFileOutput("azure_output.wav"))
{
using (var synthesizer = new SpeechSynthesizer(config, audioConfig))
{
string text = "这是保存为本地文件的Azure TTS演示内容";
var result = await synthesizer.SpeakTextAsync(text);
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine("音频文件保存成功");
}
}
}
}
}
}
两种方案对比
以下是两种文字转语音方案的详细对比,方便开发者根据需求选择:
| 对比维度 | System.Speech | Azure TTS |
|---|---|---|
| 依赖环境 | 仅需Windows系统,无需额外服务 | 需要联网,依赖Azure服务 |
| 音色数量 | 仅系统预装音色,数量有限 | 支持140+语言和多种神经网络音色 |
| 语音质量 | 普通合成音,自然度一般 | 神经网络音色,自然度高 |
| 成本 | 免费 | 按调用量收费,有免费额度 |
| 适用场景 | 本地轻量应用、离线场景 | 高质量语音需求、跨平台应用 |
方案选择建议
如果你的应用是运行在Windows平台的本地程序,对语音质量要求不高,且不需要联网,优先选择System.Speech方案,实现简单且无额外成本。
如果你的应用需要高质量的自然语音,支持多语言多音色,或者需要跨平台运行,那么Azure TTS是更好的选择,虽然需要联网和一定的成本,但能提供更好的用户体验。
在实际开发中,也可以根据场景混合使用两种方案,比如离线时使用System.Speech,联网时切换到Azure TTS,兼顾可用性和体验。
C#System.SpeechAzure_TTS文字转语音修改时间:2026-07-22 17:33:35