DEV Community

Cover image for Using Speech Synthesis Utterance to enhance user experience
Abraham covenant
Abraham covenant

Posted on

Using Speech Synthesis Utterance to enhance user experience

Introduction

Imagine being able to speak or write to your computer, phone, or other device and have it understand and respond to you in natural-sounding language. This is the promise of speech synthesis technology, and at the heart of this technology is the Speech Synthesis Utterance interface. In this article, we'll explore what the Speech Synthesis Utterance interface is, how it works, and the many ways it can be used to create custom text-to-speech applications that can improve accessibility, communication, and more.

What is Speech Synthesis Utterance?

Speech synthesis, also known as text-to-speech technology, is the process of generating human-like speech from written or typed text. This technology has a wide range of applications, including assistive technology for people with disabilities, language translation, virtual assistants, and more.

Using Speech Synthesis Utterance , developers can create applications that can read out text to users. This can be useful in a variety of situations, such as providing accessibility for individuals with visual impairments, creating language learning tools, or building text-to-speech applications for use in call centers or other environments.

How to use Speech Synthesis Utterance

To use Speech Synthesis Utterance , developers first need to check if the user's browser supports the Web Speech API. If it does, they can create a new Speech Synthesis Utterance object and set its text property to the string of text they want to be read aloud. They can also set additional properties, such as the voice or rate, to customize the speech output.

To check if you browser supports the web speech API simply enter the line of code below.



if ('speechSynthesis' in window) {
  // Create a new SpeechSynthesisUtterance object
}else{
  //display error
}


Enter fullscreen mode Exit fullscreen mode

Alternatively you can use the try catch method



try {
  const utterance = new SpeechSynthesisUtterance();
  // The Web Speech API is supported by the browser.
  // You can use the API to synthesize speech from text.
} catch (error) {
  // The Web Speech API is not supported by the browser.
  // You cannot use the API to synthesize speech from text.
}


Enter fullscreen mode Exit fullscreen mode

Once the Speech Synthesis Utterance object has been created and configured, developers can use the speech Synthesis interface to play the speech. This can be done by calling the speak() method on the speech Synthesis object and passing in the Speech Synthesis Utterance object as an argument.



// First, check if the browser supports the Web Speech API
if ('speechSynthesis' in window) {
    // Create a new SpeechSynthesisUtterance object
    const utterance = new SpeechSynthesisUtterance();

    // Set the text of the utterance
    utterance.text = "Hello world!";

   // Set additional properties, such as the voice or rate, if 
   desired
   utterance.voice = speechSynthesis.getVoices()[0];


   // Use the speechSynthesis interface to play the speech
   speechSynthesis.speak(utterance);
}


Enter fullscreen mode Exit fullscreen mode

Now you might be tempted to just check it out immediately and run only the code above , it may not work and you won't see any error in your browsers console only just a warning check below

Image showing warning

The image above shows a browser console with a warning message indicating that a function must be called in order to use the Web Speech API. To fix this problem, you can turn the code into a function and use an event in JavaScript, such as the onclick event, to handle calling the function.



function saySomething(){
  // First, check if the browser supports the Web Speech API
  if ('speechSynthesis' in window) {
    // Create a new SpeechSynthesisUtterance object
    const utterance = new SpeechSynthesisUtterance();

    // Set the text of the utterance
    utterance.text = "Hello world!";

   // Set additional properties, such as the voice or rate, if 
  desired
  utterance.voice = speechSynthesis.getVoices()[3];


  // Use the speechSynthesis interface to play the speech
  speechSynthesis.speak(utterance);
 }
}

document.getElementById("myButton").addEventListener('click',saySmething)



Enter fullscreen mode Exit fullscreen mode

Then put a basic button element in your html with the Id of myButton



<button id="myButton">click to say something</button>


Enter fullscreen mode Exit fullscreen mode

if you've followed the above steps , it works just fine lets move on.

In addition to text-to-speech, the Web Speech API also includes speech recognition capabilities which we will cover in my next article. This allows developers to build applications that can convert spoken words into text. Together, these two technologies can be used to create powerful and engaging user experiences on the web, To go further visit the documentation.

Overall, Speech Synthesis Utterance is a valuable tool for developers looking to add text-to-speech functionality to their web applications. By using this API, they can create applications that can read out text to users, providing a more accessible and engaging user experience.

I am Abraham Covenant, and I am a full-stack developer with a passion for building beautiful and functional web applications. I have a strong understanding of HTML, CSS, and JavaScript, as well as experience with popular CSS frameworks such as Tailwind CSS and Bootstrap5. I am also proficient in React,Solidity and PHP and I enjoy using these technologies to create scalable and responsive front-end interfaces.

I am always looking for ways to improve and expand my skills, and I am eager to learn new technologies and techniques to stay at the forefront of web development.

If you are looking for a full-stack developer , I would love to speak with you about how I can be of help. Thank you
My email for personal discussions

Top comments (1)

Collapse
 
leonardpuettmann profile image
Leonard Püttmann

Cool articles Abraham, very good insights!