我正在创建语音控制的 React 应用程序,该应用程序将在语音命令 ON 和 OFF 时使用“react-speech-recognition”模块打开和关闭灯。一切都按预期工作,但是我面临的一个问题是,即使我说出了一次命令,该应用程序也在听我的声音两次或更多次。如果有人对此分享他们的建议,那就太好了。这是我的代码,
//This component will listen my command (ON/OFF) and pass the corresponding command value 28/-28
// to the next component which is RetrieveCommand
// The problem is, even I'm saying 'ON/OFF' once, in the console log printing I can see the APP is
// listening it twice or more
import React from 'react';
import { Button, Row,Col } from 'antd';
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition';
import RetrieveCommand from './RetrieveCommand';
const StartApplication = () => {
const buttonDiv = {
marginTop : '100px',
marginLeft : '400px',
};
const lampOffDiv = {
border: '5px solid #ffebcc',
backgroundColor: '#666666',
height : '300px',
width : '60%',
marginLeft: '200px',
};
const lampOnDiv = {
border: '5px solid #ffebcc',
backgroundColor: '#adebad',
height : '300px',
width : '60%',
marginLeft: '200px',
};
enum BUTTON {
START = "Start Application",
STOP = "Stop Application"
};
enum LAMP_STATE{
ON = 28,
OFF = -28,
INITIAL = 0
}
const [buttonName, setButtonName] = React.useState(BUTTON.START);
const [commandNumber, setCommandNumber] = React.useState(LAMP_STATE.INITIAL);
const [lightOnOnce, setLightOnOnce] = React.useState(false);
const [lightOffOnce, setLightOffOnce] = React.useState(false);
React.useEffect(() => {
if (!SpeechRecognition.browserSupportsSpeechRecognition()) {
alert("Ups, your browser is not supported!");
}
}, []);
const onRegex = new RegExp('^on$','i');
const offRegex = new RegExp('^off$','i');
const commands = [
{
command: onRegex,
callback: () => {
console.log("on command");
if(commandNumber === LAMP_STATE.ON){
setLightOnOnce(false);
}else{
setCommandNumber(LAMP_STATE.ON);
setLightOnOnce(true);
}
},
matchInterim: true
},
{
command: offRegex,
callback: () => {
if(lightOnOnce){
if(commandNumber === LAMP_STATE.OFF){
setLightOffOnce(false);
}else{
setCommandNumber(LAMP_STATE.OFF);
setLightOffOnce(true);
}
}
},
matchInterim: true
},
]
const { transcript } = useSpeechRecognition({commands});
//console.log("this is transcript :",transcript);
const handleListening = () => {
if(buttonName === BUTTON.START){
console.log("button start is clicked");
setButtonName(BUTTON.STOP);
SpeechRecognition.startListening({ continuous: true });
}else if(buttonName === BUTTON.STOP){
SpeechRecognition.stopListening();
setCommandNumber(LAMP_STATE.OFF);
setLightOnOnce(false);
if(lightOffOnce){
setLightOffOnce(false);
}else{
setLightOffOnce(true);
}
setButtonName(BUTTON.START);
setCommandNumber(LAMP_STATE.INITIAL);
}
}
return(
<>
<div style={commandNumber === LAMP_STATE.ON ? lampOnDiv : lampOffDiv}>
{((commandNumber === LAMP_STATE.ON && lightOnOnce) || (commandNumber === LAMP_STATE.OFF && lightOffOnce)) && <RetrieveCommand commandNumber={commandNumber}/>}
</div>
<div style={buttonDiv}>
<Button type="primary" size={'large'} onClick={handleListening}>{buttonName}</Button>
</div>
</>
);
}
export default StartApplication;
所以,我注意到例如“on”命令的回调至少被调用两次,即使我只说一次“ON”。
有人可以帮我吗?有没有可能我说一次,命令就执行一次?
非常感谢您的光临!