androidreact-nativespeech-to-textreact-native-voice

react-native-voice - Speech To Text not working in android


I am using both react-native-voice and expo-speech libraries to transcript my voice and to convert a text to a speech. The problem is, in android only, when I start the code runs Voice.onSpeechStarts then it returns false and does not record anything. But in iOS it is working fine. I am also using expo speech to speak out some component immediate after recording voice.

package.json versions:

"react": "18.2.0",
"react-native": "0.71.0",
"@react-native-voice/voice": "^3.2.4",

Note: I have tried with voice versions : 3.1.5, 3.2.4
Android sdk version : 31

Code:

export const Screen = () => {
  const [isRecording, setIsRecording] = useState(false);
  const [userMessage, setUserMessage] = useState('');

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

  const onSpeechStartHandler = e => {
    console.log('start handler=»', e);
  };

  const onSpeechEndHandler = e => {
    console.log('stop handler', e);
  };
  const onSpeechResultsHandler = e => {
    console.log('speech result handler', e);
    setUserMessage(e.value[0]);
  };

  const startRecording = async () => {
    setIsRecording(true);
    try {
      await Voice.start('en-US');
    } catch (e) {
      console.log('error -> ', e);
    }
  };

  const stopRecording = () => {
    setIsRecording(false);
    try {
      Voice.stop();
      console.log(userMessage);
    } catch (e) {
      console.log('error -> ', e);
    }
  };

  return (
    <View
      style={{
        alignContent: 'center',
        justifyContent: 'center',
        backgroundColor: 'black',
        flex: 1,
      }}>
      <Button
        title={isRecording ? 'Stop Speaking' : 'Start Speaking'}
        onPress={isRecording ? stopRecording : startRecording}
      />
    </View>
  );
};

Error after running the code

When I try to check available speech services

Thanks for your time.


Solution

  • It was working after I installed some android 13 sdk from android studio and verified react native doctor output.

    If it was android 13 sdk issue it would have worked earlier when I tested it with android 11 but it didn't worked at that moment. So, I don't know the exact issue which I solved which made it work on my android 13 and 11 devices. It just started working from a point.

    So, anyone who is reading this, I would suggest you to at least verify the react-native doctor output, it may solve your issue.

    I will post anything if I am able to find out what made it worked, in the comments and here : https://github.com/react-native-voice/voice/issues/429

    Thanks.