我想在Chrome中使用window.SpeechSynthesis的cancel方法,以切断一个话语并开始一个新的话语(因此您不必听到仍在队列中的所有话语)
var test = new SpeechSynthesisUtterance("Test");
window.speechSynthesis.speak(test);
window.speechSynthesis.cancel();
var test2 = new SpeechSynthesisUtterance("Test2");
window.speechSynthesis.speak(test2);
字符串
预期:使用var test启动语音,但由于cancel()而立即取消。然后使用var test2再次启动语音,这应该可以正常工作。
好吧,当然这没有发生。但是发生的事情什么也不是。:D看起来好像在cancel()之后调用speak()没有什么作用。
API说明如下:
此方法从队列中删除所有发音。如果正在说出发音,则立即停止说话。此方法不会更改全局SpeechSynthesis示例的暂停状态。
Thx for answers:)
4条答案
按热度按时间eni9jsuy1#
我刚刚遇到了同样的问题,在一个取消后发出一个发言将导致没有发言。
我在**clear()**调用后添加了一个小的超时(250 ms),它似乎可以工作:
字符串
aoyhnmkz2#
它现在似乎工作,使用您提供的代码。
u91tlkcl3#
字符串
yuvru6vn4#
这段代码直接取自p5speech库的例子。也许这有助于解决这个问题?
字符串