SpeechSynthesisEvent

Baseline 已广泛支持

此功能已成熟,并可在多种设备和浏览器版本上使用。自 2018 年 9 月以来,它已在各种浏览器中推出。

Web Speech API 的 SpeechSynthesisEvent 接口包含有关语音服务中已处理的 SpeechSynthesisUtterance 对象当前状态的信息。

Event SpeechSynthesisEvent

构造函数

SpeechSynthesisEvent()

创建一个新的 SpeechSynthesisEvent

实例属性

SpeechSynthesisEvent 接口还继承了其父接口 Event 的属性。

SpeechSynthesisEvent.charIndex 只读

返回事件触发时,在 SpeechSynthesisUtterance.text 中正在朗读的字符的索引位置。

SpeechSynthesisEvent.charLength 只读

如果语音引擎支持,则返回 charIndex 位置之后剩余要朗读的字符数。如果语音引擎无法提供此信息,则返回 0。

SpeechSynthesisEvent.elapsedTime 只读

返回事件触发时,在 SpeechSynthesisUtterance.text 开始朗读后经过的秒数。

SpeechSynthesisEvent.name 只读

返回与 SpeechSynthesisUtterance.text 正在朗读时发生的某些类型事件相关的名称:对于 mark 事件,是遇到的 SSML 标记的名称;对于 boundary 事件,是遇到的边界的类型。

SpeechSynthesisEvent.utterance 只读

返回触发事件的 SpeechSynthesisUtterance 实例。

实例方法

SpeechSynthesisEvent 接口还继承了其父接口 Event 的方法。

示例

js
utterThis.onpause = (event) => {
  const char = event.utterance.text.charAt(event.charIndex);
  console.log(
    `Speech paused at character ${event.charIndex} of "${event.utterance.text}", which is "${char}".`,
  );
};

utterThis.onboundary = (event) => {
  console.log(
    `${event.name} boundary reached after ${event.elapsedTime} seconds.`,
  );
};

规范

规范
Web Speech API
# speechsynthesisevent

浏览器兼容性

另见