这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

SpeechSynthesis 接口在 Web 语音 API is the controller interface for the speech service; this can be used to retrieve information about the synthesis voices available on the device, start and pause speech, and other commands besides.

特性

SpeechSynthesis also inherits properties from its parent interface, EventTarget .

SpeechSynthesis.paused 只读
布尔 that returns true SpeechSynthesis object is in a paused state.
SpeechSynthesis.pending 只读
布尔 that returns true if the utterance queue contains as-yet-unspoken utterances.
SpeechSynthesis.speaking 只读
布尔 that returns true if an utterance is currently in the process of being spoken — even if SpeechSynthesis is in a paused state.

方法

SpeechSynthesis also inherits methods from its parent interface, EventTarget .

SpeechSynthesis.cancel()

Removes all utterances from the utterance queue.

SpeechSynthesis.getVoices()
Returns a list of SpeechSynthesisVoice objects representing all the available voices on the current device.
SpeechSynthesis.pause()
放置 SpeechSynthesis object into a paused state.
SpeechSynthesis.resume()
放置 SpeechSynthesis object into a non-paused state: resumes it if it was already paused.
SpeechSynthesis.speak()
添加 utterance to the utterance queue; it will be spoken when any other utterances queued before it have been spoken.

事件

Listen to this event using addEventListener() 或通过把事件监听器赋值给 on eventname 特性为此接口。

voiceschanged
Fired when the list of SpeechSynthesisVoice objects that would be returned by the SpeechSynthesis.getVoices() method has changed.
也可用凭借 onvoiceschanged 特性。

范例

First, a simple example:

let utterance = new SpeechSynthesisUtterance("Hello world!");
speechSynthesis.speak(utterance);
						

Now we'll look at a more fully-fledged example. In our Speech synthesiser demo , we first grab a reference to the SpeechSynthesis controller using window.speechSynthesis . After defining some necessary variables, we retrieve a list of the voices available using SpeechSynthesis.getVoices() and populate a select menu with them so the user can choose what voice they want.

Inside the inputForm.onsubmit handler, we stop the form submitting with preventDefault() ,  create a new SpeechSynthesisUtterance instance containing the text from the text <input> , set the utterance's voice to the voice selected in the <select> element, and start the utterance speaking via the SpeechSynthesis.speak() 方法。

var synth = window.speechSynthesis;
var inputForm = document.querySelector('form');
var inputTxt = document.querySelector('.txt');
var voiceSelect = document.querySelector('select');
var pitch = document.querySelector('#pitch');
var pitchValue = document.querySelector('.pitch-value');
var rate = document.querySelector('#rate');
var rateValue = document.querySelector('.rate-value');
var voices = [];
function populateVoiceList() {
  voices = synth.getVoices();
  for(i = 0; i < voices.length ; i++) {
    var option = document.createElement('option');
    option.textContent = voices[i].name + ' (' + voices[i].lang + ')';
    if(voices[i].default) {
      option.textContent += ' -- DEFAULT';
    }
    option.setAttribute('data-lang', voices[i].lang);
    option.setAttribute('data-name', voices[i].name);
    voiceSelect.appendChild(option);
  }
}
populateVoiceList();
if (speechSynthesis.onvoiceschanged !== undefined) {
  speechSynthesis.onvoiceschanged = populateVoiceList;
}
inputForm.onsubmit = function(event) {
  event.preventDefault();
  var utterThis = new SpeechSynthesisUtterance(inputTxt.value);
  var selectedOption = voiceSelect.selectedOptions[0].getAttribute('data-name');
  for(i = 0; i < voices.length ; i++) {
    if(voices[i].name === selectedOption) {
      utterThis.voice = voices[i];
    }
  }
  utterThis.pitch = pitch.value;
  utterThis.rate = rate.value;
  synth.speak(utterThis);
  inputTxt.blur();
}
						

规范

规范 状态 注释
Web 语音 API
The definition of 'SpeechSynthesis' in that specification.
草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
SpeechSynthesis Chrome 33 Edge ≤18 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0
cancel Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0
getVoices Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0
onvoiceschanged Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 3.0
pause Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 注意事项
4.4.3 注意事项
In Android, pause() ends the current utterance. pause() behaves the same as cancel() .
Chrome Android 33 注意事项
33 注意事项
In Android, pause() ends the current utterance. pause() behaves the same as cancel() .
Firefox Android 62 注意事项
62 注意事项
In Android, pause() ends the current utterance. pause() behaves the same as cancel() .
61 — 62 注意事项 Disabled
In Android, pause() ends the current utterance. pause() behaves the same as cancel() .
Disabled ). To change preferences in Firefox, visit about:config.
Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0 注意事项
3.0 注意事项
In Android, pause() ends the current utterance. pause() behaves the same as cancel() .
paused Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0
pending Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0
resume Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0
speak Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0
speaking Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0
voiceschanged event Chrome 33 Edge 14 Firefox 49 IE 不支持 No Opera 21 Safari 7 WebView Android 4.4.3 Chrome Android 33 Firefox Android 62 Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改:
  1. SpeechSynthesis
  2. 特性
    1. onvoiceschanged
    2. paused
    3. pending
    4. speaking
  3. 方法
    1. cancel()
    2. getVoices()
    3. pause()
    4. resume()
    5. speak()
  4. 事件
    1. voiceschanged
  5. Related pages for Web Speech API
    1. SpeechGrammar
    2. SpeechGrammarList
    3. SpeechRecognition
    4. SpeechRecognitionAlternative
    5. SpeechRecognitionError
    6. SpeechRecognitionEvent
    7. SpeechRecognitionResult
    8. SpeechRecognitionResultList
    9. SpeechSynthesisErrorEvent
    10. SpeechSynthesisEvent
    11. SpeechSynthesisUtterance
    12. SpeechSynthesisVoice

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1