DEV Community

Cover image for How to Use Text To Speech In Chrome with JS
Mafee7
Mafee7

Posted on

8 4

How to Use Text To Speech In Chrome with JS

image
Text To Speech [Speech Synthesis API]

API Usage:

JavaScript

var T2S; 

if("speechSynthesis" in window || speechSynthesis){ // Checking If speechSynthesis Is Supported.

    var text = prompt("What Text To Say?") || `Text To Speech is Over Powered`; // Ask What To Say or use Default

    T2S = window.speechSynthesis || speechSynthesis; // Storing speechSynthesis API as variable - T2S
    var utter = new SpeechSynthesisUtterance(text); // To Make The Utterance
    T2S.speak(utter); // To Speak The Utterance

    window.onbeforeunload = function(){
        T2S.cancel(); // To Stop Speaking If the Page Is Closed.
    }

}
Enter fullscreen mode Exit fullscreen mode

Pls React & Save!

Top comments (1)

Collapse
 
astagi profile image
Andrea Stagi

It works on Edge and Firefox as well. Do you know how to make it working on Safari?

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay