Search code examples
react-nativetext-to-speechspeech-to-text

React Native - Cannot use Speech To Text and Text To Speech together


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, when i end registering my voice and start a speech with expo-voice, there is no sound. It seems like react-native-voice completly mutes the audio when the voice recording is ended. The speech starts, but i must press on the mic button (activating the voice recognition) to hear it.

The only way i found to make everything work together is by stopping the voice recording after the text to speech has ended. Here is a part of the code :

  const startRecognizing = async () => {
    setButtonColor('#38b000');
    // Starts listening for speech for a specific locale
    try {
      await Voice.start('en-EN');
    } catch (e) {
      console.error(e);
    }
  };

  const destroyRecognizer = async () => {
    //Destroys the current SpeechRecognizer instance
    try {
      await Voice.destroy();
    } catch (e) {
      console.error(e);
    }
  };

  const stopRecognizing = async () => {
    setButtonColor('#344E41');
    setTimeout(() => {
      Speech.speak("I did not understand, can you repeat please ?", {
        language: 'en-EN',
        onDone: () => {
          setTimeout(
            async() => {
              await destroyRecognizer();
            }, 1000);
        },
      });
    }, 1000);
  };
  return (
    <View style={styles.microphoneButtonContainer}>
        <TouchableOpacity
        onPressIn={startRecognizing}
        onPressOut={stopRecognizing}>
        <Image
            style={styles.microphoneButton}
            source={require('../img/microphone-icon.png')}
            />
        </TouchableOpacity>
    </View>
    );

This solution brings a lot of edgecases so i can't work with it. None of the methods given to stop the recording solve the issue. And i did not found any help in the libraries docs. Is there a solution to this ? Thank you for your help !


Solution

  • Because there was no way to solve the issue with react-native-voice methods, i had the idea to go search directly in the library if i can modify the native code. For ios the code is in ios/voice/Voice.m. I found this :

    - (void) teardown {
        self.isTearingDown = YES;
        [self.recognitionTask cancel];
        self.recognitionTask = nil;
        
        // Set back audio session category
        [self resetAudioSession];
    

    So i tried to comment out [self resetAudioSession];, i then rebuilt the packages with npx-pod-install (i use cocoapod), and it worked!

    Doing this may cause edgecases, i did not fully test the methods yet, and i did not try for android.