mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-20 02:16:39 +00:00
Update TextToSpeechButton.tsx
This commit is contained in:
1 parent
91690ff99a
commit
fcb6bec474
1 file changed
+18
-15
@@ -1,6 +1,7 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import Speaker from '../assets/speaker.svg?react';
|
import Speaker from '../assets/speaker.svg?react';
|
||||||
import Stopspeech from '../assets/stopspeech.svg?react';
|
import Stopspeech from '../assets/stopspeech.svg?react';
|
||||||
|
import EasySpeech from 'easy-speech';
|
||||||
|
|
||||||
export default function SpeakButton({
|
export default function SpeakButton({
|
||||||
text,
|
text,
|
||||||
@@ -14,26 +15,28 @@ export default function SpeakButton({
|
|||||||
const [isSpeaking, setIsSpeaking] = useState(false);
|
const [isSpeaking, setIsSpeaking] = useState(false);
|
||||||
const [isSpeakHovered, setIsSpeakHovered] = useState(false);
|
const [isSpeakHovered, setIsSpeakHovered] = useState(false);
|
||||||
|
|
||||||
const handleSpeakClick = (text: string) => {
|
const handleSpeakClick = async (text: string) => {
|
||||||
if (isSpeaking) {
|
if (isSpeaking) {
|
||||||
window.speechSynthesis.cancel();
|
EasySpeech.cancel();
|
||||||
setIsSpeaking(false);
|
setIsSpeaking(false);
|
||||||
return;
|
return;
|
||||||
} // Stop ongoing speech if already speaking
|
} // Stop ongoing speech if already speaking
|
||||||
|
|
||||||
const utterance = new SpeechSynthesisUtterance(text);
|
try {
|
||||||
setIsSpeaking(true);
|
await EasySpeech.init(); // Initialize EasySpeech
|
||||||
|
setIsSpeaking(true);
|
||||||
utterance.onend = () => {
|
|
||||||
setIsSpeaking(false); // Reset when speech ends
|
EasySpeech.speak({
|
||||||
};
|
text,
|
||||||
|
onend: () => setIsSpeaking(false), // Reset when speech ends
|
||||||
utterance.onerror = () => {
|
onerror: () => {
|
||||||
console.error('Speech synthesis failed.');
|
console.error('Speech synthesis failed.');
|
||||||
setIsSpeaking(false);
|
setIsSpeaking(false);
|
||||||
};
|
},
|
||||||
|
});
|
||||||
window.speechSynthesis.speak(utterance);
|
} catch (error) {
|
||||||
|
console.error('Failed to initialize speech synthesis', error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in new issue
Block a user