2

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

这是用于集成的代码:

//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 函数,以通知服务我要停止并清除音频流?

4

2 回答 2

4

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

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

于 2017-04-08T22:42:15.903 回答
0

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

于 2017-04-09T10:27:49.477 回答