このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

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

ブラウザーの互換性

関連情報