【问题标题】:AWS - Amazon Polly Text To SpeechAWS - Amazon Polly 文本转语音
【发布时间】:2017-04-08 15:06:55
【问题描述】:

我对 “文本转语音” Amazon Polly 服务有疑问。
我已将此服务集成到我的聊天机器人中,以便口头描述机器人在聊天中向用户写入的内容。
效果还不错,但不知道能不能在she(我选的是女声)说完之前提前停止声音。有时我需要在对话中走得更远,我不想听到句子的结尾。

这是用于集成的代码:

//Html side
function textToSpeech(text) {
  $.ajax({
    type: 'GET',
    url: '/Chat/TextToSpeech?text=' + text,
    
    cache: false,
    success: function (result) {
    
      var audio = document.getElementById('botvoice');
      $("#botvoice").attr("src", "/Audios/" + result);
      audio.load();                 
      audio.play();
    }
  });
}

控制器端:

public ActionResult TextToSpeech(string text)
{
    string filename = "";
    try
    {
        AWSCredentials credentials = new StoredProfileAWSCredentials("my_credential");
        AmazonPollyClient client = new AmazonPollyClient(credentials, Amazon.RegionEndpoint.EUWest1);

        // Create describe voices request.
        DescribeVoicesRequest describeVoicesRequest = new DescribeVoicesRequest();
        // Synchronously ask Amazon Polly to describe available TTS voices.
        DescribeVoicesResponse describeVoicesResult = client.DescribeVoices(describeVoicesRequest);
        List<Voice> voices = describeVoicesResult.Voices;


        // Create speech synthesis request.
        SynthesizeSpeechRequest synthesizeSpeechPresignRequest = new SynthesizeSpeechRequest();
        // Text
        synthesizeSpeechPresignRequest.Text = text;
        // Select voice for synthesis.
        synthesizeSpeechPresignRequest.VoiceId = voices[18].Id;
        // Set format to MP3.
        synthesizeSpeechPresignRequest.OutputFormat = OutputFormat.Mp3;
        // Get the presigned URL for synthesized speech audio stream.

        string current_dir = AppDomain.CurrentDomain.BaseDirectory;
        filename = CalculateMD5Hash(text) + ".mp3";
        var path_audio = current_dir + @"\Audios\" + filename;

        var presignedSynthesizeSpeechUrl = client.SynthesizeSpeechAsync(synthesizeSpeechPresignRequest).GetAwaiter().GetResult();

        FileStream wFile = new FileStream(path_audio, FileMode.Create);
        presignedSynthesizeSpeechUrl.AudioStream.CopyTo(wFile);
        wFile.Close();
    }
    catch (Exception ex)
    {
        filename = ex.ToString();
    }

    return Json(filename, JsonRequestBehavior.AllowGet);
}

我的聊天中存在一个输入文本(显然),用于编写和发送(通过按键盘上的 ENTER)问题给机器人。我试图将命令audio.src="" 放入处理程序中,她停止说话,但聊天仍然被阻止......它似乎在等待音频流的结束。我必须刷新页面才能看到新消息和回复。

是否有任何我可以使用特定参数集调用的 Amazon 函数,以通知服务我要停止并清除音频流?

【问题讨论】:

    标签: c# amazon-web-services text-to-speech amazon-polly


    【解决方案1】:

    Amazon Polly 返回一个 .mp3 文件。它不负责播放音频文件。

    您在播放/停止音频时遇到的任何困难都是您用于播放 MP3 音频文件的代码造成的。它与 Amazon Polly 服务本身无关。

    【讨论】:

      【解决方案2】:

      谢谢!
      我发现了真正的问题:当我停止音频时,我没有打印出其余的消息。我将调用添加到在聊天中打印消息的函数。为了停止声音,我使用了命令 audio.src="";

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-17
        • 1970-01-01
        • 1970-01-01
        • 2019-08-12
        • 2018-10-27
        • 2023-03-14
        • 2018-01-03
        相关资源
        最近更新 更多