gpt4 book ai didi

javascript - SpeechSynthesis - Onend 在语音结束前触发

转载 作者:行者123 更新时间:2023-12-03 04:12:14 31 4
gpt4 key购买 nike

我想创建一个与过去的 GoBarbara.com 类似的页面。文本字段的内容将从文本转换为语音。

因此,我想使用 SpeechSynthesis API (javaScript),它在触发 intro.mp3 后执行文本到语音转换。播放完语音后,应播放 end.mp3。这是由 intro.mp3 和演讲末尾的“onend”类型的事件监听器处理的。

播放音频的基本触发方案是 1 -> 2 -> 3。其中1是intro.mp3,2是speech,3是end.mp3。

我当前的代码如下:

function doSpeech() {
var speech = new SpeechSynthesisUtterance();
speech.text = getText();
speech.lang = "en-US";
speech.voice = speechSynthesis.getVoices().filter(function(voice) {return voice.name == 'Google UK English Male'; })[0];
speechSynthesis.speak(speech);
speech.onend = playEnd();
}

要触发 end.mp3,我想使用 SpeechSynthesis 拥有的 onend 事件。但是 onend 在实际演讲结束之前就触发了 end.mp3!?

看来这可能是一个错误。但除此之外,如何让 mp3 在文本读完后正确启动?注意:语音部分的长度可以不同。

最佳答案

正确的代码是

function doSpeech() {
var speech = new SpeechSynthesisUtterance();
speech.text = getText();
speech.lang = "en-US";
speech.voice = speechSynthesis.getVoices().filter(function(voice) {return voice.name == 'Google UK English Male'; })[0];
speech.onend = playEnd();
speechSynthesis.speak(speech);
}

speak之前使用onend

关于javascript - SpeechSynthesis - Onend 在语音结束前触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44279458/

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