SpeechSynthesis: speak() メソッド
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2018年9月.
speak() は SpeechSynthesis インターフェイスのメソッドで、発話 (utterance) を発話キューに追加します。この発話は、その前にキューに入れられている他のすべての発話が読み上げられた後に読み上げられます。
構文
js
speak(utterance)
引数
utterance-
SpeechSynthesisUtteranceオブジェクトです。
返値
なし (undefined)。
例
このスニペットは、音声合成デモ(ライブで見る)から抜粋したものです。読み上げたいテキストが含まれたフォームが送信されると、(他の処理と併せて)このテキストを含む新しい発話を生成し、それを speak() の引数として渡すことで読み上げます。
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();
};
仕様書
| Specification |
|---|
| Web Speech API> # dom-speechsynthesis-speak> |