AWS - Amazon Polly 文本转语音

AWS - Amazon Polly Text To Speech

我对 "text-to-speech" 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 函数,以便通知服务我要停止并清除音频流?

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

您遇到的任何困难playing/stopping 音频都是您用来播放 MP3 音频文件的代码的结果。它与 Amazon Polly 服务本身无关。

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