Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/useSpeechSynthesis.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const useSpeechSynthesis = (props = {}) => {
const [voices, setVoices] = useState([]);
const [speaking, setSpeaking] = useState(false);
const [supported, setSupported] = useState(false);
const [paused, setPaused] = useState(false);

const processVoices = (voiceOptions) => {
setVoices(voiceOptions);
Expand All @@ -27,6 +28,7 @@ const useSpeechSynthesis = (props = {}) => {

const handleEnd = () => {
setSpeaking(false);
setPaused(false);
onEnd();
};

Expand Down Expand Up @@ -56,14 +58,32 @@ const useSpeechSynthesis = (props = {}) => {
const cancel = () => {
if (!supported) return;
setSpeaking(false);
setPaused(false);
window.speechSynthesis.cancel();
};

const pause = () => {
if (!supported) return;
setSpeaking(false);
setPaused(true);
window.speechSynthesis.pause();
};

const resume = () => {
if (!supported) return;
setSpeaking(true);
setPaused(false);
window.speechSynthesis.resume();
};

return {
supported,
speak,
speaking,
cancel,
pause,
paused,
resume,
voices,
};
};
Expand Down