0

Text to Speech Custom Web API I have tested using postman able to return Memorystream in local and able play audio. but unable to return Memorystream after hosting in Azure getting 502 error.

I will place the response in src of Audio HTML tag. enter image description here

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Speech.Synthesis;
using System.Globalization;
using System.Net.Http.Headers;
using System.Web;

namespace WebApplication4.Controllers
{
    public class TextToSpeechController : ApiController
    {
        [System.Web.Http.Route("GetVoice/{Text}")]
        [HttpGet]
        public HttpResponseMessage Get(string Text)
        {
            //AudioStreem au = new AudioStreem();    
            try
            {
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                var audioStream = GetAudio(Text);
                audioStream.Position = 0;
                response.Content = new StreamContent(audioStream);
          response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("audio/wav");
               
                return response;
              
            }
            catch (Exception ex)
            {
                var res = new HttpResponseMessage();
                res.StatusCode = HttpStatusCode.BadRequest;
                return res; ;
            }

        }
        private static MemoryStream GetAudio(string input)
        {
            MemoryStream audioStream = new MemoryStream();

            var t = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            {
                SpeechSynthesizer synthesizer = new SpeechSynthesizer();
                synthesizer.SetOutputToDefaultAudioDevice();
                synthesizer.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Teen, 0, CultureInfo.GetCultureInfo("en-gb"));
                synthesizer.SetOutputToWaveStream(audioStream);

                //add a space between all characters to spell it out.
                //string val = String.Join<char>(" ", input);
                synthesizer.Speak(input);

            }));

            t.Start();
            t.Join();

            return audioStream;
        }

    }
}
  • After querying the application logs, I tried to solve it. Later, I located the permission problem. I suggest that you can use cognitive-services to achieve the function you want, or use vm to deploy your program. Choose according to your own actual situation. – Jason Pan Jun 07 '21 at 02:24
  • Does my answer useful to you ? – Jason Pan Jun 07 '21 at 14:21

1 Answers1

0

After testing and accessing information, this problem should not be solved.

Reason:

  1. As Eric Brown said,

    (How to get System.Speech on windows azure websites?)

System.Speech is a desktop API, and definitely isn't supported on server systems. Microsoft.Speech.Synthesis is a server API and is supported on (standalone) servers. However, I'm not sure if it's possible to deploy this on Azure websites, as it requires extensive updates to the installed software (for voices, etc.).

  1. And azure app services running in sandbox environment, the problem occurred because of insufficient authority to operate the registry.

    Related articles and posts:

    1. ASP.NET Web Application - on deploy, System.Speech.dll object not getting set to an instance of an object

    2. System.Speech.dll object not getting set to an instance of

    3. System.Speech.Synthesis.SpeechSynthesizer (SAPY) in ASP.NET Application Not working after publishing in Local IIS

Suggestion

In summary, it is recommended to try something such as https://www.microsoft.com/cognitive-services/en-us/speech-api which is intended for a web usage.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29