chat / client /src /hooks /Input /useTextToSpeechBrowser.ts
Zhitomir's picture
chat init
aa2e6af
Raw
History Blame Contribute Delete
658 Bytes
import { useState } from 'react';
function useTextToSpeechBrowser() {
const [isSpeaking, setIsSpeaking] = useState(false);
const generateSpeechLocal = (text: string) => {
const synth = window.speechSynthesis;
synth.cancel();
const utterance = new SpeechSynthesisUtterance(text);
utterance.onend = () => {
setIsSpeaking(false);
};
setIsSpeaking(true);
synth.speak(utterance);
};
const cancelSpeechLocal = () => {
const synth = window.speechSynthesis;
synth.cancel();
setIsSpeaking(false);
};
return { generateSpeechLocal, cancelSpeechLocal, isSpeaking };
}
export default useTextToSpeechBrowser;