Convert Text to Speech in multiple languages using Asp.Net Core & C#

In this article, we are going to learn how to convert text to speech in multiple languages using one of the important Cognitive Services API called Microsoft Text to Speech Service API ( One of the API in Speech API ).

C# (1013.0 KB)
265 times
Add to favorites
E-mail Twitter Digg Facebook

Solution explorer

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace TextToSpeechApp.BusinessLayer
    public class Authentication
        //Token url
        public static readonly string fetchTokenUri =
        private string subscriptionKey;
        private string authToken;

        public Authentication(string subscriptionKey)
            this.subscriptionKey = subscriptionKey;
            this.authToken = FetchTokenAsync(fetchTokenUri, subscriptionKey).Result;

        public string GetAccessToken()
            return this.authToken;

        /// <summary>
        /// Fetch auth token from the Azure
        /// </summary>
        /// <param name="fetchUri">Token url endpoint</param>
        /// <param name="subscriptionKey">Azure speech subscription key</param>
        /// <returns></returns>
        private async Task<string> FetchTokenAsync(string fetchUri, string subscriptionKey)
            using (var client = new HttpClient())
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
                UriBuilder uriBuilder = new UriBuilder(fetchUri);

                var result = await client.PostAsync(uriBuilder.Uri.AbsoluteUri, null);

                return await result.Content.ReadAsStringAsync();