-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(*): add functionality for listen button
- Loading branch information
1 parent
115a975
commit ddd92d7
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { DisplayItem } from '../../Layout/Text/styledText'; | ||
import { AiFillPlayCircle, AiFillPauseCircle, AiFillStop } from 'react-icons/ai'; | ||
|
||
const TextToSpeech = ({ text }) => { | ||
const [isPaused, setIsPaused] = useState(false); | ||
const [utterance, setUtterance] = useState(null); | ||
|
||
useEffect(() => { | ||
const synth = window.speechSynthesis; | ||
const u = new SpeechSynthesisUtterance(text); | ||
|
||
setUtterance(u); | ||
setIsPaused(true) | ||
|
||
return () => { | ||
synth.cancel(); | ||
}; | ||
}, [text]); | ||
|
||
const handlePlay = () => { | ||
const synth = window.speechSynthesis; | ||
const voices = synth.getVoices(); | ||
|
||
utterance.voice = voices[15]; | ||
utterance.rate = 0.95; | ||
|
||
if (isPaused) { | ||
synth.resume(); | ||
} | ||
|
||
synth.speak(utterance); | ||
|
||
setIsPaused(false); | ||
}; | ||
|
||
const handlePause = () => { | ||
const synth = window.speechSynthesis; | ||
|
||
synth.pause(); | ||
|
||
setIsPaused(true); | ||
}; | ||
|
||
const handleStop = () => { | ||
const synth = window.speechSynthesis; | ||
|
||
synth.cancel(); | ||
|
||
setIsPaused(true); | ||
}; | ||
|
||
return ( | ||
<div style={{display: 'flex'}}> | ||
{ | ||
isPaused ? | ||
<DisplayItem Green onClick={handlePlay}> | ||
<AiFillPlayCircle style={{fontSize: 'larger'}}/> | ||
{isPaused ? "Listen" : "Resume"} | ||
</DisplayItem> | ||
: | ||
<DisplayItem Green onClick={handlePause}> | ||
<AiFillPauseCircle style={{fontSize: 'larger'}}/> | ||
Pause | ||
</DisplayItem> | ||
|
||
} | ||
| ||
<DisplayItem Red onClick={handleStop}> | ||
<AiFillStop style={{fontSize: 'larger'}}/> | ||
Stop | ||
</DisplayItem> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TextToSpeech; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters