react-native-voice/voice

Is there a way I can use to determine if the microphone permission has been granted or not?

Closed this issue · 1 comments

Is there a way I can use to determine if the microphone permission has been granted or not?

Info.plist
<key>NSMicrophoneUsageDescription</key> <string>This app requires the permission of microphone</string> <key>NSSpeechRecognitionUsageDescription</key> <string>This app requires the permission of microphone</string>

export const SpeechRecognition = (props: any) => {

  useEffect(() => {
    Voice.onSpeechStart = startListening;
    Voice.onSpeechEnd = stopListening;
    Voice.onSpeechResults = onSpeechResults;
    Voice.onSpeechError = (err) =>
      console.error('err in chatai component', err);
    return () => {
      Voice.destroy().then(Voice.removeAllListeners);
    };
  }, []);
const onSpeechResults = (e: any) => {
    const text = e.value[0];
    setRecognizedText(text);
  };  
const startListening = async () => {
    try {
      await Voice.start('en-US');
      setIsListening(true);
    } catch (error) {
      console.error('Error starting voice recognition', error);
    }
  };
  const stopListening = async () => {
    try {
      await Voice.stop();
      setIsListening(false);
    } catch (error) {
      console.error('Error stopping voice recognition', error);
    }
  };
  }

Use React Native permission lib