phanpy/src/utils/speech.js

16 lines
372 B
JavaScript
Raw Normal View History

2023-12-21 13:17:14 +03:00
export const supportsTTS = 'speechSynthesis' in window;
export function speak(text, lang) {
if (!supportsTTS) return;
try {
if (speechSynthesis.speaking) {
speechSynthesis.cancel();
}
const utterance = new SpeechSynthesisUtterance(text);
if (lang) utterance.lang = lang;
speechSynthesis.speak(utterance);
} catch (e) {
alert(e);
}
}