AI文本朗读应用(二)

本文档介绍了如何调用API实现文本转语音(TTS)功能,详细讲解了创建Windows桌面应用程序的过程,包括添加Authentication.cs和TTSApi.cs类,以及在Form1.cs中实现生成、播放和保存语音的逻辑。同时,提出了程序复现和增加从文件读取文本的挑战,并提供了相关的人工智能在线教程和书籍推荐。
摘要由CSDN通过智能技术生成

调用api实现TTS

注:如对api的使用有任何疑问,可以查阅文本转语音 REST API

  1. 选择右侧“解决方案资源管理器”中的“TTS_Demo”,右键选择“添加”->“新建项”。

    选择“类”,名称为“Authentication.cs”,点击“添加”。

  2. Authentication.cs 文件中,引用如下命名空间。

    using System.Net.Http;
    using System.IO;

    添加如下代码。

    namespace TTS_Demo
    {
        public class Authentication
        {
            private string subscriptionKey;
            private string tokenFetchUri;
    ​
            public Authentication(string tokenFetchUri, string subscriptionKey)
            {
                if (string.IsNullOrWhiteSpace(tokenFetchUri))
                {
                    throw new ArgumentNullException(nameof(tokenFetchUri));
                }
                if (string.IsNullOrWhiteSpace(subscriptionKey))
                {
                    throw new ArgumentNullException(nameof(subscriptionKey));
                }
                this.tokenFetchUri = tokenFetchUri;
                this.subscriptionKey = subscriptionKey;
            }
    ​
            public async Task<string> FetchTokenAsync()
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", this.subscriptionKey);
                    UriBuilder uriBuilder = new UriBuilder(this.tokenFetchUri);
    ​
                    var result = await client.PostAsync(uriBuilder.Uri.AbsoluteUri, null).ConfigureAwait(false);
                    return await result.Content.ReadAsStringAsync().ConfigureAwait(false);
                }
            }
    ​
        }
    }

  3. 同理,新建类文件 TTSApi.cs,并添加如下代码。

    namespace TTS_Demo
    {      
        class TTSApi
        {
            //语言配置信息
            string locale = "zh-CN";
            string voiceName = "Microsoft Server Speech Text to Speech Voice (zh-CN, HuihuiRUS)";
        
            string accessToken;
            Authentication auth = new Authentication("https://westus.api.cognitive.microsoft.com/sts/v1.0/issuetoken", "REPLACE_WITH_YOUR_KEY");
            string host = "https://westus.tts.speech.microsoft.com/cognitiveservices/v1";
    ​
            //转换文本并保存
            public async Task textToSpeechAsync(string text, string savePath)
            {
                try
                {
                    accessToken = await auth.FetchTokenAsync().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
    ​
                string body = "<speak version='1.0' xmlns='https://www.w3.org/2001/10/synthesis' xml:lang='"+locale+"'>"
                +"<voice n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值