Using Speech Synthesis Utterance to enhance user experience
Tags: webdev, javascript, programming, tutorial
Published: 2022-12-13 04:16:23


## 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
}
```
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.
}
```
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);
}
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzsyszl7s3me572891zc.gif)

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)

```
Then put a basic button element in your html with the Id of `myButton`
```
<button id="myButton">click to say something</button>
```
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](https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance).

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](AbrahamCovenant2004@gmail.com)