SpeechSynthesis: phương thức speak()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2018.
Phương thức speak() của giao diện SpeechSynthesis thêm một utterance vào hàng đợi utterance; nó sẽ được nói khi mọi utterance khác đã được xếp trước nó được nói xong.
Cú pháp
js
speak(utterance)
Tham số
utterance-
Một đối tượng
SpeechSynthesisUtterance.
Giá trị trả về
Không có (undefined).
Ví dụ
Đoạn mã này được trích từ Speech synthesizer demo của chúng tôi (xem trực tiếp). Khi một form chứa văn bản mà chúng ta muốn nói được gửi đi, chúng ta, trong số các việc khác, tạo một utterance mới chứa văn bản này, rồi nói nó bằng cách truyền nó vào speak() như một tham số.
js
const synth = window.speechSynthesis;
// …
inputForm.onsubmit = (event) => {
event.preventDefault();
const utterThis = new SpeechSynthesisUtterance(inputTxt.value);
const selectedOption =
voiceSelect.selectedOptions[0].getAttribute("data-name");
for (const voice of voices) {
if (voice.name === selectedOption) {
utterThis.voice = voice;
}
}
synth.speak(utterThis);
inputTxt.blur();
};
Đặc tả kỹ thuật
| Specification |
|---|
| Web Speech API> # dom-speechsynthesis-speak> |