react-native-voice / voice

:microphone: React Native Voice Recognition library for iOS and Android (Online and Offline Support)
MIT License
1.83k stars 494 forks source link

Cant stop speeching recognizer #309

Open PepeValenzuela98 opened 3 years ago

PepeValenzuela98 commented 3 years ago
const App = () => {
  const [recognized, setRecognized] = useState('');
  const [pitch, setPitch] = useState('');
  const [error, setError] = useState('');
  const [end, setEnd] = useState('');
  const [started, setStarted] = useState('');
  const [results, setResults] = useState([]);
  const [partialResults, setPartialResults] = useState([]);

  useEffect(() => {
    const onSpeechStart = e => {
      setStarted('√');
    };

    const onSpeechRecognized = e => {
      setRecognized('√');
    };

    const onSpeechEnd = e => {
      setEnd('√');
    };

    const onSpeechError = e => {
      setError(JSON.stringify(e.error));
    };

    const onSpeechResults = e => {
      setResults(e.value);
    };

    const onSpeechPartialResults = e => {
      setPartialResults(e.value);
    };

    const onSpeechVolumeChanged = e => {
      setPitch(e.value);
    };

    Voice.onSpeechStart = onSpeechStart;
    Voice.onSpeechRecognized = onSpeechRecognized;
    Voice.onSpeechEnd = onSpeechEnd;
    Voice.onSpeechError = onSpeechError;
    Voice.onSpeechResults = onSpeechResults;
    Voice.onSpeechPartialResults = onSpeechPartialResults;
    Voice.onSpeechVolumeChanged = onSpeechVolumeChanged;

    return () => {
      Voice.destroy().then(Voice.removeAllListeners);
    };
  }, []);

  const startRecognizing = async () => {
    setRecognized('');
    setPitch('');
    setError('');
    setStarted('');
    setResults([]);
    setPartialResults([]);
    setEnd('');
    try {
      await Voice.start('es-MX');
    } catch (error) {
      console.error(error);
    }
  };

  const stopRecognizing = async () => {
    try {
      await Voice.stop();
    } catch (error) {
      console.error(error);
    }
  };

  const cancelRecognizing = async () => {
    try {
      await Voice.cancel();
    } catch (error) {
      console.error(error);
    }
  };

  const destroyRecognizer = async () => {
    try {
      await Voice.destroy();
    } catch (error) {
      console.error(error);
    }
    setRecognized('');
    setPitch('');
    setError('');
    setStarted('');
    setResults([]);
    setPartialResults([]);
    setEnd('');
  };
  return (
    <ScrollView contentContainerStyle={styles.container}>
      <Text style={styles.welcome}>Voice testing</Text>
      <Text styles={styles.instructions}>
        Press the button and start speaking.
      </Text>
      <Text style={styles.stat}>{`Started: ${started}`}</Text>
      <Text style={styles.stat}>{`Recognized: ${recognized}`}</Text>
      <Text style={styles.stat}>{`Pitch: ${pitch}`}</Text>
      <Text style={styles.stat}>{`Error: ${error}`}</Text>

      <Text style={styles.stat}>Results</Text>
      {results.map((result, i) => (
        <Text key={i} style={styles.stat}>
          {result}
        </Text>
      ))}

      <Text style={styles.stat}>Partial Results</Text>
      {partialResults.map((partialResult, i) => (
        <Text key={i} style={styles.stat}>
          {partialResult}
        </Text>
      ))}

      <Text style={styles.stat}>{`End: ${end}`}</Text>

      <TouchableHighlight onPress={startRecognizing}>
        <Image
          style={styles.button}
          source={{
            uri:
              'https://icons-for-free.com/iconfiles/png/512/microphone+icon-1320183704865921193.png',
          }}
        />
      </TouchableHighlight>

      <TouchableHighlight onPress={stopRecognizing}>
        <Text style={styles.action}>Stop Recognizing</Text>
      </TouchableHighlight>

      <TouchableHighlight onPress={cancelRecognizing}>
        <Text style={styles.action}>Cancel</Text>
      </TouchableHighlight>

      <TouchableHighlight onPress={destroyRecognizer}>
        <Text style={styles.action}>Destroy</Text>
      </TouchableHighlight>
    </ScrollView>
  );
};

const styles = StyleSheet.create({
  button: {
    width: 50,
    height: 50,
  },
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  action: {
    textAlign: 'center',
    color: '#0000FF',
    marginVertical: 5,
    fontWeight: 'bold',
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  stat: {
    textAlign: 'center',
    color: '#B0171F',
    marginBottom: 1,
  },
});

export default App;
Aung-Myint-Thein commented 3 years ago

What was the error?

Loki899899 commented 2 years ago

I believe the error here was the speech recognition doesn't stop even after Voice.stop() is called. I am currently experiencing this. Here I have tried implementing it https://github.com/Loki899899/test-rnvoice, please correct me if anything is wrong, anything is helpful.