我对“文本到语音”的 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 函数,以通知服务我要停止并清除音频流?