1. Начало
  2. API
  3. Using text to speech APIs with JavaScript
API

Using text to speech APIs with JavaScript

Cliff Weitzman

Клиф Вайцман

Главен изпълнителен директор и основател на Speechify

Speechify API осигурява 300 ms латентност, естествени човешки гласове и поддръжка на над 50 езика

apple logoApple Design Award 2025
50M+ потребители

Using a text-to-speech (TTS) API with JavaScript can greatly enhance the user experience of web applications. The Web Speech API, supported by modern browsers like Chrome and Firefox, provides a powerful set of tools for speech synthesis and speech recognition. In this tutorial, we will explore how to integrate a TTS API into your JavaScript code, convert text to speech, customize speech settings, and utilize available voices.

Getting started with JavaScript and Text to Speech

To get started, you'll need a basic understanding of HTML, CSS, and JavaScript. Begin by creating an HTML file and linking your JavaScript file using the script src tag. In your JavaScript file, initialize the speech synthesis object and set up an event listener for when the voices are ready. const synth = window.speechSynthesis; // Wait for voices to be loaded synth.onvoiceschanged = () => { const voices = synth.getVoices(); // Do something with the available voices }; Once the voices are loaded, you can access them using the synth.getVoices() method. This will return a list of available voices that you can use for speech synthesis. You can loop through the voices using forEach and display them in your HTML. const voiceSelect = document.getElementById('voice-select'); voices.forEach((voice) => { const option = document.createElement('option'); option.textContent = ${voice.name} (${voice.lang}); option.setAttribute('value', voice.lang); voiceSelect.appendChild(option); }); Next, you can create a function to synthesize speech from the selected voice. This function takes the text input from a textarea element and uses the selected voice to generate speech. const speak = () => { const text = document.getElementById('text-input').value; const voice = voices[voiceSelect.selectedIndex]; const utterance = new SpeechSynthesisUtterance(text); utterance.voice = voice; synth.speak(utterance); }; Add an event listener to the button or form submit to trigger the speak function. const button = document.getElementById('speak-button'); button.addEventListener('click', speak); With these few lines of code, you can convert text to speech in real-time. Customize the speech rate, pitch, and volume by setting properties on the SpeechSynthesisUtterance object. utterance.rate = 0.8; utterance.pitch = 1; utterance.volume = 1; As you continue to explore the Web Speech API, you'll find additional features for speech recognition and controlling speech synthesis events. Remember to consult the official documentation for more details and attributions.

Seamlessly integrate with Speechify

When it comes to using a text-to-speech API with JavaScript, Speechify stands out as the best choice. With its seamless integration with the Web Speech API, Speechify makes it incredibly easy to convert text to speech in real-time. Its comprehensive documentation and user-friendly tutorials provide step-by-step guidance, making it ideal for both novice and experienced developers in web development. With Speechify, you have access to a wide range of available voices and can customize speech settings such as speech rate and pitch. Whether you're a front-end developer or a software developer, Speechify is the perfect tool to enhance your web applications and create engaging user experiences. In conclusion, using a text-to-speech API with JavaScript opens up a world of possibilities for web development. By integrating speech synthesis into your projects, you can create engaging and accessible user experiences. Whether you're a front-end developer or a software developer, learning how to leverage the Web Speech API will enhance your skillset and enable you to build dynamic applications. So why not give it a try and bring your webpages to life with the power of text-to-speech.

Достъпвайте любимите си гласове на Speechify чрез API – бързо, мащабируемо и удобно за разработчици

Вземете достъп до API
api access banner

Споделете тази статия

Cliff Weitzman

Клиф Вайцман

Главен изпълнителен директор и основател на Speechify

Клиф Вайцман е застъпник за хора с дислексия и е главен изпълнителен директор и основател на Speechify — приложението номер 1 в света за преобразуване на текст в реч, с над 100 000 петзвездни отзива и първо място в App Store в категорията „Новини и списания“. През 2017 г. Вайцман е включен в престижния списък Forbes 30 под 30 за приноса си към това интернет да бъде по-достъпен за хора с обучителни затруднения. Клиф Вайцман е представян в EdSurge, Inc., PC Mag, Entrepreneur, Mashable и много други водещи медии.

speechify logo

За Speechify

#1 четец за текст към реч

Speechify е водещата в света платформа за текст към реч, на която се доверяват над 50 милиона потребители и която има повече от 500 000 петзвездни отзива за своите приложения за текст към реч за iOS, Android, разширение за Chrome, уеб приложение и настолно приложение за Mac. През 2025 година Apple отличи Speechify с престижната Apple Design Award на WWDC, определяйки я като „ключов ресурс, който помага на хората да живеят по-добре“. Speechify предлага над 1000 естествено звучащи гласа на над 60 езика и се използва в близо 200 държави. Сред известните гласове са Snoop Dogg и Гуинет Полтроу. За създатели и бизнеси Speechify Studio предоставя напреднали инструменти, включително AI генератор на гласове, AI клониране на глас, AI дублаж и AI променящ глас. Speechify също задвижва водещи продукти със своето висококачествено и достъпно като цена API за текст към реч. Представено в The Wall Street Journal, CNBC, Forbes, TechCrunch и други водещи медии, Speechify е най-големият доставчик на услуги за текст към реч в света. Посетете speechify.com/news, speechify.com/blog и speechify.com/press, за да научите повече.