【问题标题】:Unable to return Audio MemoryStreem in Web API .Net 5无法在 Web API .Net 5 中返回音频 MemoryStreem
【发布时间】:2021-06-07 10:21:50
【问题描述】:

文本到语音自定义 Web API 我已经使用邮递员进行了测试,该邮递员能够在本地返回 Memorystream 并能够播放音频。 但在 Azure 中托管后无法返回 Memorystream,出现 502 错误。

我会将响应放在 Audio HTML 标签的 src 中。 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;
        }

    }
}

【问题讨论】:

  • 查询应用日志后,尝试解决。后来,我找到了权限问题。我建议你可以使用cognitive-services来实现你想要的功能,或者使用vm来部署你的程序。根据自己的实际情况选择。
  • 我的回答对你有用吗?

标签: c# asp.net-web-api azure-web-app-service text-to-speech


【解决方案1】:

经过测试和查阅资料,这个问题应该没有解决。

原因:

  1. 正如埃里克·布朗所说,

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

System.Speech 是一个桌面 API,服务器系统绝对不支持。 Microsoft.Speech.Synthesis 是一种服务器 API,在(独立)服务器上受支持。但是,我不确定是否可以在 Azure 网站上部署它,因为它需要对已安装的软件(语音等)进行大量更新。

  1. 而azure app服务在沙盒环境下运行,由于注册表操作权限不足而出现问题。

    相关文章和帖子:

    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

建议

总而言之,建议尝试使用 https://www.microsoft.com/cognitive-services/en-us/speech-api 之类的东西,它是为网络使用而设计的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2021-05-13
    • 2021-06-23
    • 1970-01-01
    • 2020-09-09
    • 2021-09-24
    相关资源
    最近更新 更多