C#调用微软api文本转语音

发布时间 2023-08-18 13:29:24作者: WebEnh

目录

1.注册微软云服务,搭建文本转语音标准应用(每月500万字免费好像)

2.Visual studio使用nuget给程序安装Microsoft.CognitiveServices.Speech框架

3.引用命名空间

4.文本转语音参考代码

5.文本转语音下载到本地参考代码

1.注册微软云服务,搭建文本转语音标准应用(每月500万字免费好像)


2.Visual studio使用nuget给程序安装Microsoft.CognitiveServices.Speech框架

 


3.引用命名空间
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
4.文本转语音参考代码
static string YourSubscriptionKey = "你的应用密匙";
static string YourServiceRegion = "应用密匙下面的区域";
async static Task Main(string[] args)
{
//创建登陆验证的配置文件
var speechConfig = SpeechConfig.FromSubscription(YourSubscriptionKey,YourServiceRegion);
//设置语音合成角色为云溪
speechConfig.SpeechSynthesisVoiceName = "zh-CN-YunxiNeural";
//创建请求对象,填入配置
using (var speechSynthesizer = new SpeechSynthesizer(speechConfig))
{
//创建要转语音的内容
string text = "hello";
//等待响应自动播放声音,注意保持网络流畅,太卡可能不行
await speechSynthesizer.SpeakTextAsync(text);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
5.文本转语音下载到本地参考代码
static string YourSubscriptionKey = "你的应用密匙";
static string YourServiceRegion = "应用密匙下面的区域";
async static Task Main(string[] args)
{
var speechConfig = SpeechConfig.FromSubscription(YourSubscriptionKey,YourServiceRegion);
//创建保存路径的配置文件
var audioConfig = AudioConfig.FromWavFileOutput("test.mp4");
speechConfig.SpeechSynthesisVoiceName = "zh-CN-YunxiNeural";
//创建请求对象,写入配置
using (var speechSynthesizer = new SpeechSynthesizer(speechConfig, audioConfig))
{
string text = "hello";
//等待响应,不会播放,会直接保存文件到上面指定路径
await speechSynthesizer.SpeakTextAsync(text);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}

————————————————
版权声明:本文为CSDN博主「林传凯」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46616558/article/details/127109060