WebSpeech 语音合成:暂停话语 1,播放另一个话语 2,然后恢复话语 1 - 可能吗?

WebSpeech Speech Synthesis: Pausing utterance1, playing another utterance2, and resuming utterance1 - possible?

我正在使用 WebSpeech 的 speechSynthesis 模块让 Web 应用程序说话。但是,您似乎只能将话语添加到队列中,然后对整个队列进行 pause()、resume() 和 cancel()。

我有一种情况,我想有两个话语:

utterance1 = new SpeechSynthesisUtterance(text1);
utterance2 = new SpeechSynthesisUtterance(text2);

我想播放 utterance1,然后在中间暂停,播放 utterance2,然后继续播放 utterance1。在代码中,它看起来像这样:

speechSynthesis.speak(utterance1);
// ... after a while
speechSyntehsis.pause(utterance1);
speechSynthesis.speak(utterance2);
// ... after a long while
speechSynthesis.resume(utterance1);

不幸的是,speechSynthesis 的方法 pause()、resume() 和 cancel() 不接受任何参数,而是作用于整个语音发声队列。有什么办法可以实现这种行为吗?

如果我可以有多个 speechSynthesis 对象,那么我可以为每个话语创建一个,但我相信我只能有一个。

如果我能跟踪话语在字符串中的位置 "been uttered to" 然后我可以取消它然后用文本的其余部分创建一个新话语,但我不知道那是否是可能。

有什么建议吗?

我已经使用我的库 Artyom.js 在 speechSynthesis 中工作了几个月,并且根据文档(以及我所做的所有测试)暂停单个合成实例并重新验证另一个实例是不可能的,因为所有实例都与 window.speechSynthesis 有关(如果有一天 API 发生变化,那将是语音合成的又一大步)。当你调用speechSynthesis的pause方法时"instance",它会申请所有队列,没有其他办法。

根据 documentation :

// the only solution would be if the speechSynthesis official API had a constructor like
// and a real NEW instance be created
// var synthRealInstance = new speechSynthesis();
// but till the date ... nope :(

var synthA =  window.speechSynthesis;
var synthB = 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.');

synthA.speak(utterance1);
synthB.speak(utterance2);

synthA.pause();
// or synthB will anyway stop the synthesis of the queue

在话语 (onmark) 上有一个 属性 但是没有很好的记录并且可能不会起作用,因为这个 api 仍然是实验性的。

The mark event is fired when a ‘mark’ tag is reached in a Speech Synthesis Markup Language (SSML) file. Just know that it’s possible to pass your speech data to an utterance using an XML-based SSML document. The main advantage of this being that it makes it easier to manage speech content when building applications that have large amount of text that need to be synthesised.

Read more about here.