这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
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()
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
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. |
草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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
注意事项
|
Chrome Android
33
注意事项
|
Firefox Android
62
注意事项
|
Opera Android 不支持 No | Safari iOS 7 |
Samsung Internet Android
3.0
注意事项
|
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 |
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。
SpeechSynthesis