mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-25 11:48:14 +03:00
16 lines
372 B
JavaScript
16 lines
372 B
JavaScript
|
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);
|
||
|
}
|
||
|
}
|