SpeechSynthesis: speak() メソッド
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。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();
};
仕様書
| 仕様書 |
|---|
| Web Speech API> # dom-speechsynthesis-speak> |