Skip to content

参考

基于 MDN Web Docs 与 W3C WebAudio Community Group 规范草案(2026 年)编写

SpeechSynthesis(TTS)

接口与入口

名称说明
window.speechSynthesis全局 SpeechSynthesis 实例(入口)
SpeechSynthesis控制器,继承 EventTarget
SpeechSynthesisUtterance一次朗读请求(文本+参数)
SpeechSynthesisVoice一个可用语音
SpeechSynthesisEvent朗读相关事件对象
SpeechSynthesisErrorEvent朗读错误事件对象

SpeechSynthesis 方法

方法说明
speak(utterance)把 utterance 加入队列,依次朗读
cancel()清空队列并停止
pause()暂停当前朗读
resume()恢复暂停的朗读
getVoices()返回 SpeechSynthesisVoice[](可能空,待 voiceschanged

SpeechSynthesis 只读状态

属性类型含义
pausedbool是否暂停
pendingbool队列是否有待朗读
speakingbool是否正在朗读

SpeechSynthesis 事件

事件触发
voiceschangedgetVoices() 结果变化(异步加载完成)
start / end朗读开始/全部结束
error出错

SpeechSynthesisUtterance

构造:new SpeechSynthesisUtterance(text)

属性类型范围/默认说明
textstring-要朗读的文本
langstringBCP-47zh-CN / en-US
voiceSpeechSynthesisVoice-选定语音(优先于 lang)
volumefloat0 – 1(默认 1)音量
ratefloat0.1 – 10(默认 1)语速
pitchfloat0 – 2(默认 1)音调

事件:start / end / pause / resume / mark / boundary / error

SpeechSynthesisVoice

属性说明
name语音名称
langBCP-47 语言
voiceURI唯一标识
default是否系统默认
localService是否本地引擎(false=云端)

SpeechRecognition(STT)

接口

名称说明
SpeechRecognition控制器(Chrome/Edge 需 webkit 前缀)
SpeechRecognitionResultList识别结果列表
SpeechRecognitionResult单条结果(含多个候选)
SpeechRecognitionAlternative单个候选(transcript + confidence)
SpeechRecognitionEventresult 事件对象
SpeechRecognitionErrorEventerror 事件对象
SpeechGrammar已废弃,无效果
SpeechGrammarList已废弃,无效果

构造(兼容前缀)

javascript
const SpeechRecognition =
  window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();

SpeechRecognition 属性

属性默认说明
lang浏览器默认BCP-47 识别语言
continuousfalsetrue 持续识别;false 单次
interimResultsfalse是否返回中间结果
maxAlternatives1候选数
grammars-已废弃,无效

SpeechRecognition 方法

方法说明
start()开始识别(须用户手势触发)
stop()停止(等当前结果返回)
abort()立即中止

SpeechRecognition 事件

事件触发
audiostart开始采集音频
audioend结束采集音频
speechstart检测到语音
speechend语音结束
soundstart / soundend检测到声音
nomatch有语音但无匹配
result拿到识别结果
error出错
start / end识别开始/结束

result 事件结构

text
SpeechRecognitionEvent
 └─ results: SpeechRecognitionResultList
     └─ [i]: SpeechRecognitionResult
         ├─ isFinal: boolean
         └─ [j]: SpeechRecognitionAlternative
             ├─ transcript: string
             └─ confidence: float (0-1)

error 类型

error含义
no-speech未检测到语音
aborted主动 abort
audio-capture麦克风硬件问题
network网络故障(Chrome 云端识别)
not-allowed麦克风权限被拒
service-not-allowed服务不允许(策略)
bad-grammargrammar 错误(已废弃仍会触发)
language-unsupportedlang 不支持

浏览器兼容性详表

SpeechSynthesis

浏览器支持备注
Chrome33+全功能
Edge14+全功能
Firefox49+全功能
Safari7+(iOS)/ 7+(macOS)全功能
Samsung Internet4.0+全功能
Opera21+全功能

状态:Baseline Widely available(2018-09)。

SpeechRecognition

浏览器支持备注
Chrome25+(webkit云端识别引擎
Edge79+(webkitChromium 内核
Safari14.1+(webkit不完整
Opera27+(webkitChromium
Samsung Internet1.5+
Firefox默认禁用about:configmedia.webspeech.recognition.enable + force_enable(实验性)

状态:Limited availability(非 Baseline)。

安全与权限

要求说明
安全上下文HTTPS 或 localhost(SpeechRecognition 必须)
麦克风权限getUserMedia 同源权限,首次弹窗授权
Permissions-Policymicrophone 指令;iframe 需 allow="microphone"
用户手势start() 必须由手势触发(防自动监听)
传输Chrome STT 音频上传服务器(非本地)

第三方方案对比

方案类型浏览器离线精度成本
原生 SpeechRecognition云 STTChromium/Safari免费
原生 SpeechSynthesis本地/云 TTS全部部分免费
OpenAI Whisper API云 STT全部按分钟
AssemblyAI云 STT全部按分钟
whisper.cpp WASM本地 STT全部(WASM)免费
Vosk本地 STT全部(WASM)免费
Azure Speech云 STT/TTS全部按量
ElevenLabs云 TTS全部极高(拟真)按字符
Coqui TTS本地 TTS全部(WASM)免费

资源链接