SpeechSynthesis

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

SpeechSynthesis interface of the 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 只读
A Boolean that returns true SpeechSynthesis object is in a paused state.
SpeechSynthesis.pending 只读
A Boolean that returns true if the utterance queue contains as-yet-unspoken utterances.
SpeechSynthesis.speaking 只读
A Boolean 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()
Puts the SpeechSynthesis object into a paused state.
SpeechSynthesis.resume()
Puts the SpeechSynthesis object into a non-paused state: resumes it if it was already paused.
SpeechSynthesis.speak()
Adds an utterance to the utterance queue; it will be spoken when any other utterances queued before it have been spoken.

Events

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.
Also available via the 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();
}
					

规范

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

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
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 From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). 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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
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
62
61 — 62
Disabled
Disabled From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
Opera Android 不支持 No Safari iOS 7 Samsung Internet Android 3.0

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。
用户必须明确启用此特征。

另请参阅