Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. SpeechSynthesis

SpeechSynthesis

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⁩.

TheSpeechSynthesis interface of theWeb Speech 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.

EventTarget SpeechSynthesis

Instance properties

SpeechSynthesis also inherits properties from its parent interface,EventTarget.

SpeechSynthesis.pausedRead only

A boolean value that returnstrue if theSpeechSynthesis object is in a paused state.

SpeechSynthesis.pendingRead only

A boolean value that returnstrue if the utterance queue contains as-yet-unspoken utterances.

SpeechSynthesis.speakingRead only

A boolean value that returnstrue if an utterance is currently in the process of being spoken — even ifSpeechSynthesis is in a paused state.

Instance methods

SpeechSynthesis also inherits methods from its parent interface,EventTarget.

SpeechSynthesis.cancel()

Removes all utterances from the utterance queue.

SpeechSynthesis.getVoices()

Returns a list ofSpeechSynthesisVoice objects representing all the available voices on the current device.

SpeechSynthesis.pause()

Puts theSpeechSynthesis object into a paused state.

SpeechSynthesis.resume()

Puts theSpeechSynthesis object into a non-paused state: resumes it if it was already paused.

SpeechSynthesis.speak()

Adds anutterance to the utterance queue; it will be spoken when any other utterances queued before it have been spoken.

Events

Listen to this event usingaddEventListener() or by assigning an event listener to theoneventname property of this interface.

voiceschanged

Fired when the list ofSpeechSynthesisVoice objects that would be returned by theSpeechSynthesis.getVoices() method has changed.Also available via theonvoiceschanged property.

Examples

First, an example:

js
let utterance = new SpeechSynthesisUtterance("Hello world!");speechSynthesis.speak(utterance);

Now we'll look at a more fully-fledged example. In ourSpeech synthesizer demo, we first grab a reference to the SpeechSynthesis controller usingwindow.speechSynthesis. After defining some necessary variables, we retrieve a list of the voices available usingSpeechSynthesis.getVoices() and populate a select menu with them so the user can choose what voice they want.

Inside theinputForm.onsubmit handler, we stop the form submitting withpreventDefault(), create a newSpeechSynthesisUtterance 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 theSpeechSynthesis.speak() method.

js
const synth = window.speechSynthesis;const inputForm = document.querySelector("form");const inputTxt = document.querySelector(".txt");const voiceSelect = document.querySelector("select");const pitch = document.querySelector("#pitch");const pitchValue = document.querySelector(".pitch-value");const rate = document.querySelector("#rate");const rateValue = document.querySelector(".rate-value");let voices = [];function populateVoiceList() {  voices = synth.getVoices();  for (const voice of voices) {    const option = document.createElement("option");    option.textContent = `${voice.name} (${voice.lang})`;    if (voice.default) {      option.textContent += " — DEFAULT";    }    option.setAttribute("data-lang", voice.lang);    option.setAttribute("data-name", voice.name);    voiceSelect.appendChild(option);  }}populateVoiceList();if (speechSynthesis.onvoiceschanged !== undefined) {  speechSynthesis.onvoiceschanged = populateVoiceList;}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;    }  }  utterThis.pitch = pitch.value;  utterThis.rate = rate.value;  synth.speak(utterThis);  inputTxt.blur();};

Specifications

Specification
Web Speech API
# tts-section

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp