如何取消反应原生语音中的停止功能



我有一个项目在react native,主要思想是-语音文本从用户。我有一个问题:如果用户做了一个短暂的停顿,那么允许将语音翻译成文本。我怎样才能通过这个考试?下列代码

useEffect(() => {
Voice.onSpeechError = onSpeechError;
Voice.onSpeechResults = onSpeechResults;
return () => {-
Voice.destroy().then(Voice.removeAllListeners);

}
}, []);
const startSpeechToText = async () => {
await Voice.start("en-US");
};

const stopSpeechToText = async () => {
await Voice.stop();
setStarted(false);
};

也许您可以收听onSpeechEndonSpeechPartialResults之类的事件,如果发生,则重新开始识别。但在这种情况下,你需要停止按钮或任何方式来停止识别。

你也可以检查这个问题,它与你的问题有关https://github.com/react-native-voice/voice/issues/107

下面的代码片段可能会对您有所帮助

useFocusEffect(
useCallback(() => {
Voice.onSpeechStart = onSpeechStartHandler;
Voice.onSpeechEnd = onSpeechEndHandler;
Voice.onSpeechResults = onSpeechResultsHandler;
return () => {
Voice.destroy().then(Voice.removeAllListeners);
};
}, []),
);

const onSpeechEndHandler = (e) => {
updateState({
isVoiceRecord: false,
});
};
const onSpeechResultsHandler = (e) => {
let text = e.value[0];

_onVoiceStop();
};
const _onVoiceListen = async () => {
const langType = languages?.primary_language?.sort_code;
updateState({
isVoiceRecord: true,
});
try {
await Voice.start(langType);
} catch (error) {}
};
const _onVoiceStop = async () => {
updateState({
isVoiceRecord: false,
});
try {
await Voice.stop();
} catch (error) {
console.log('error raised', error);
}
};

最新更新