skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

    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.


  2. This version of react native voice/voice is not compatible with the android version that is above 12 as per the open issue referred to in the GitHub,
    please refer to this https://github.com/react-native-voice/voice/issues/429
    either you need to make changes on the native side of the library (android) or try downgrading the version of your android, you might have to wait for the update…
    I hope it helps you

    Login or Signup to reply.
  3. in my case i do this solution:
    node_modules-> react-native-voice/voice -> android -> open androidManifest and add this block

    <queries>
            <intent>
                <action android:name="android.speech.RecognitionService" />
            </intent>
        </queries>
    

    I tested on Android 13 device, try this ✌️

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search