gpt4 book ai didi

javascript - SpeechSynthesis.speaking 从不真实

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

我尝试用 javascript 实现一个小网页,允许从两个 mp3 之间的文本播放 specchsynthesis 部分。

至于无论出于何种原因,口语部分的 onend 语句不起作用,我想创建一个递归函数,这对我有帮助。为此,我使用了 SpeechSynthesis 的“说话”方法。但无论出于何种原因,说话永远不会是真的。

我调试并尝试了几个语句(见代码),但它从来没有被证明是真的。代码有问题吗?否则,如何报告这个库的错误?

        function doSpeech() {
var synth = window.speechSynthesis;

var utterance1 = new SpeechSynthesisUtterance('How about we say this now? This is quite a long sentence to say.');
var utterance2 = new SpeechSynthesisUtterance('We should say another sentence too, just to be on the safe side.');
synth.speak(utterance1);
if(synth.speaking){
doNothing();
}else{
playEnd();
}

playEnd() 仅在合成器说话时播放 mp3。请注意,当我将 playEnd() 放在 if 语句中时,它不会播放。我可以把任何代码放在那里,它永远不会到达,因为 synth.speaking 永远不会是真的。此示例接近 Mozilla 基础文档的示例 (https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis/speaking)。我想测试它,因为递归从未起作用。

编辑:递归在我的特定编码中仍然不会这样做。我在这里遗漏了什么吗?

  function doSpeech() {
var synth = window.speechSynthesis;
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.addEventListener('start', function(){
speechEndLoop(synth);
});


synth.speak(speech);
}

function speechEndLoop(x) {
if (x.speaking) {
speechEndLoop(x);
} else {
playEnd();
}
}

最佳答案

它工作得很好,问题是根据您的代码,您正在立即验证状态。这可能是一个问题,因为它取决于 API 如何将文本转换为音频(使用本地文本到操作系统的语音或使用谷歌服务器):

function doSpeech() {
var synth = window.speechSynthesis;

var utterance1 = new SpeechSynthesisUtterance('How about we say this now? This is quite a long sentence to say. Make it longer !');
var utterance2 = new SpeechSynthesisUtterance('We should say another sentence too, just to be on the safe side. even longer !');

synth.speak(utterance1);

// If you check immediately (js code executed in less than ms) the
// status won't be true
if (synth.speaking) {
console.log("This is usually printed, if the utterance uses the default voice of the browser (native)");
}

// Wait 500ms to check for the status of the utterance
setTimeout(function(){
if (synth.speaking) {
console.log("This will be printed if starts after 500ms :)");
}
}, 500);
}

doSpeech();

在我的例子中,两个 console.log 语句都被打印出来了。但是如果在你的情况下它没有被打印出来,那么只在话语的开始事件之后执行你的代码:

function doSpeech() {
var synth = window.speechSynthesis;
var msg = new SpeechSynthesisUtterance();
msg.text = "We should say another sentence too, just to be on the safe side. even longer !";

msg.addEventListener('start', function () {
if(synth.speaking){
console.log("This will be printed !");
}
});

synth.speak(msg);
}

doSpeech();

自己使用普通 API 非常好,但是如果您想要更强大的东西来解决文本到语音的问题,我建议您使用 use the JS Library Artyom.js ,它为语音合成 API 提供了一个非常好的包装器。即使使用这个库,您也会看到相同的行为:

function doSpeech() {
let assistant = new Artyom();

assistant.say('How about we say this now? This is quite a long sentence to say. Make it longer !', {
onStart: function() {
if(assistant.isSpeaking()){
console.log("This will be shown");
}
}
});

if(assistant.isSpeaking()){
console.log("This won't appear !");
}
}

doSpeech();

关于javascript - SpeechSynthesis.speaking 从不真实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46551705/

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