📅  最后修改于: 2023-12-03 15:16:59.480000             🧑  作者: Mango
JavaScript TTS (Text-to-Speech) is a feature that allows text to be converted to spoken audio. With JavaScript TTS, you can make your websites more interactive and enjoyable for users by adding audio support for visually impaired or people who prefer audio over text.
JavaScript TTS is usually achieved by using browser-specific APIs such as Web Speech API for Chrome and Firefox or SpeechSynthesis API for Safari. Here is an example of how to use the SpeechSynthesis API for simple text-to-speech:
// Check if browser supports SpeechSynthesis API
if ('speechSynthesis' in window) {
// Create new speech synthesis object
const speech = new SpeechSynthesisUtterance();
// Set text to be spoken
speech.text = 'Hello world!';
// Speak the text
speechSynthesis.speak(speech);
}
JavaScript TTS supports multiple languages and voices depending on the browser and operating system. You can retrieve a list of available voices using the getVoices()
method:
// Get list of available voices
const voices = window.speechSynthesis.getVoices();
// Print available voices to console
console.log(voices);
JavaScript TTS is a powerful tool for adding audio support to your website. With browser-specific APIs and support for multiple languages and voices, you can create a more pleasant and accessible experience for all users.