Chrome TTS API Complete Reference

8 min read

chrome.tts API Reference

The chrome.tts API enables extensions to synthesize speech using the operating system’s text-to-speech engine. This API is particularly useful for accessibility features, read-aloud functionality, language learning applications, and audio notifications.

Overview

The Text-to-Speech (TTS) API allows Chrome extensions to convert text into spoken words. The API provides fine-grained control over voice selection, speech rate, pitch, volume, and event handling for synchronized visual highlighting.

Permission Required: Add "tts" to the permissions array in your manifest.json:

{
  "permissions": ["tts"]
}

The API uses the system’s available TTS voices, which vary by operating system and installed language packs.

API Methods

chrome.tts.speak()

Speaks text using the TTS engine.

chrome.tts.speak(utterance, options?, callback?)

Parameters:

Example:

chrome.tts.speak('Hello, world!', {
  voiceName: 'Google US English',
  rate: 1.0,
  pitch: 1.0,
  volume: 1.0
}, () => {
  if (chrome.runtime.lastError) {
    console.error('TTS Error:', chrome.runtime.lastError);
  }
});

chrome.tts.stop()

Immediately stops any ongoing speech and clears the queue.

chrome.tts.stop()

Example:

// Stop speaking after 3 seconds
setTimeout(() => {
  chrome.tts.stop();
}, 3000);

chrome.tts.pause()

Pauses speech synthesis. Note: Not all TTS engines support pausing.

chrome.tts.pause()

chrome.tts.resume()

Resumes paused speech.

chrome.tts.resume()

chrome.tts.isSpeaking()

Checks whether the TTS engine is currently speaking.

chrome.tts.isSpeaking(callback)

Parameters:

Example:

chrome.tts.isSpeaking((speaking) => {
  console.log('Currently speaking:', speaking);
});

chrome.tts.getVoices()

Retrieves the list of available TTS voices.

chrome.tts.getVoices(callback)

Parameters:

Example:

chrome.tts.getVoices((voices) => {
  voices.forEach(voice => {
    console.log(`${voice.voiceName} (${voice.lang})`);
  });
});

TtsVoice Object

Represents a single available voice for speech synthesis.

Properties:

Example Voice Object:

{
  voiceName: 'Google US English',
  lang: 'en-US',
  remote: true,
  extensionId: 'none',
  eventTypes: ['start', 'end', 'word', 'sentence', 'marker']
}

Speech Events

The TTS API dispatches events during speech synthesis. Use the onEvent callback in chrome.tts.speak() to handle these events.

Event Types

Event Description
"start" Fired when speech begins.
"end" Fired when speech finishes.
"word" Fired at the start of each word. Includes charIndex and charLength.
"sentence" Fired at the start of each sentence. Includes charIndex and charLength.
"marker" Fires at SSML markers. Includes charIndex.
"interrupted" Fired when speech is interrupted by another speak() call or stop().
"cancelled" Fired when speech is removed from the queue before starting.
"error" Fired when an error occurs. Includes error message.

Example with Event Handling:

chrome.tts.speak('Hello world, this is a test.', {
  onEvent: (event) => {
    if (event.type === 'word') {
      console.log(`Word: "${event.charIndex}"`);
    } else if (event.type === 'end') {
      console.log('Speech finished');
    } else if (event.type === 'error') {
      console.error('TTS Error:', event.errorMessage);
    }
  }
});

Use Cases

Accessibility

Read-Aloud Features

Language Learning

Audio Notifications

Code Examples

Basic TTS Usage

Basic chrome.tts.speak Usage

function speakText(text) {
  chrome.tts.speak(text, () => {
    if (chrome.runtime.lastError) {
      console.error('TTS Error:', chrome.runtime.lastError.message);
    }
  });
}

speakText('Welcome to our extension!');

Voice Selection

function speakWithVoice(text, voiceName) {
  chrome.tts.getVoices((voices) => {
    const selectedVoice = voices.find(v => v.voiceName === voiceName);
    if (selectedVoice) {
      chrome.tts.speak(text, { voiceName: voiceName });
    } else {
      console.warn('Voice not found, using default');
      chrome.tts.speak(text);
    }
  });
}

speakWithVoice('Hello!', 'Google UK English Male');

Queue Multiple Utterances

function speakQueue(messages) {
  messages.forEach((msg, index) => {
    chrome.tts.speak(msg, {
      enqueue: true,
      onEvent: (event) => {
        if (event.type === 'end' && index === messages.length - 1) {
          console.log('All messages spoken');
        }
      }
    });
  });
}

speakQueue(['First message', 'Second message', 'Third message']);

Word Highlighting with Events

function speakWithHighlighting(text) {
  const utterance = new SpeechSynthesisUtterance(text);
  
  chrome.tts.speak(text, {
    onEvent: (event) => {
      if (event.type === 'word') {
        const word = text.substring(event.charIndex, event.charIndex + event.charLength);
        highlightWord(word); // Custom function to highlight word in UI
        console.log('Current word:', word);
      }
    }
  });
}

function highlightWord(word) {
  // Implementation depends on your UI
  console.log('Highlighting:', word);
}

Cross-References

How do I make Chrome speak text?

Use chrome.tts.speak() with the text to speak. You can customize rate, pitch, and voice selection.

Can I pause and resume speech?

Yes, use chrome.tts.pause() and chrome.tts.resume() to control ongoing speech.


Part of the Chrome Extension Guide by theluckystrike. Built at zovo.one.