skip to Main Content

I have got a project in react native, the main idea is – speech to text from user. I have one problem: If the user makes a short pause, then the translation of speech into text is allowed. How can i pass this? Code below

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);

  };

2

Answers


  1. Probably you can listen to events like onSpeechEnd or onSpeechPartialResults and if it happens start recognition again. But in this case, you need the stop button or any way to stop recognition.

    Also you can check this issue it is related to your question https://github.com/react-native-voice/voice/issues/107

    Login or Signup to reply.
  2. Below code snippet may help you in that

          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);
        }
      };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search