gpt4 book ai didi

c# - AWS - Amazon Polly 文本转语音

转载 作者:行者123 更新时间:2023-11-30 21:41:32 27 4
gpt4 key购买 nike

我对"text-to-speech" 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();
}
});
}

Controller 端:

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 返回一个 .mp3 文件。它不负责播放音频文件。

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

关于c# - AWS - Amazon Polly 文本转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43295704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com