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
28 changes: 24 additions & 4 deletions src/useSpeechSynthesis.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,39 @@ const useSpeechSynthesis = (props = {}) => {
}, []);

const speak = (args = {}) => {
const { voice = null, text = '', rate = 1, pitch = 1, volume = 1 } = args;
const { voice = null, text = '', rate = 1, pitch = 1, volume = 1, soundStart=null, soundEnd=null } = args;
if (!supported) return;
setSpeaking(true);
// Firefox won't repeat an utterance that has been
// spoken, so we need to create a new instance each time
const utterance = new window.SpeechSynthesisUtterance();
utterance.text = text;
utterance.voice = voice;
utterance.onend = handleEnd;
utterance.rate = rate;
utterance.pitch = pitch;
utterance.volume = volume;
window.speechSynthesis.speak(utterance);
// check if sound start exist
if(soundStart){
const audioStart = new Audio(soundStart);
utterance.onstart = () => {
audioStart.play();
window.speechSynthesis.pause();
audioStart.onended=function(){
window.speechSynthesis.resume();
}
}
}
// check if sound end exist
if(soundEnd){
const audioEnd = new Audio(soundEnd);
utterance.onend = () => {
handleEnd();
audioEnd.play();
}
} else {
utterance.onend = handleEnd;
}
window.speechSynthesis.speak(utterance);
};

const cancel = () => {
Expand All @@ -68,4 +88,4 @@ const useSpeechSynthesis = (props = {}) => {
};
};

export default useSpeechSynthesis;
export default useSpeechSynthesis;