import React, { Component } from 'react'; import { StyleSheet, Text, View, Image, TouchableHighlight, } from 'react-native'; import Voice, { SpeechRecognizedEvent, SpeechResultsEvent, SpeechErrorEvent, } from '@react-native-voice/voice'; type Props = {}; type State = { recognized: string; pitch: string; error: string; end: string; started: string; results: string[]; partialResults: string[]; }; class VoiceTest extends Component { state = { recognized: '', pitch: '', error: '', end: '', started: '', results: [], partialResults: [], }; constructor(props: Props) { super(props); Voice.onSpeechStart = this.onSpeechStart; Voice.onSpeechRecognized = this.onSpeechRecognized; Voice.onSpeechEnd = this.onSpeechEnd; Voice.onSpeechError = this.onSpeechError; Voice.onSpeechResults = this.onSpeechResults; Voice.onSpeechPartialResults = this.onSpeechPartialResults; Voice.onSpeechVolumeChanged = this.onSpeechVolumeChanged; } componentWillUnmount() { Voice.destroy().then(Voice.removeAllListeners); } onSpeechStart = (e: any) => { console.log('onSpeechStart: ', e); this.setState({ started: '√', }); }; onSpeechRecognized = (e: SpeechRecognizedEvent) => { console.log('onSpeechRecognized: ', e); this.setState({ recognized: '√', }); }; onSpeechEnd = (e: any) => { console.log('onSpeechEnd: ', e); this.setState({ end: '√', }); }; onSpeechError = (e: SpeechErrorEvent) => { console.log('onSpeechError: ', e); this.setState({ error: JSON.stringify(e.error), }); }; onSpeechResults = (e: SpeechResultsEvent) => { console.log('onSpeechResults: ', e); this.setState({ results: e.value, }); }; onSpeechPartialResults = (e: SpeechResultsEvent) => { console.log('onSpeechPartialResults: ', e); this.setState({ partialResults: e.value, }); }; onSpeechVolumeChanged = (e: any) => { console.log('onSpeechVolumeChanged: ', e); this.setState({ pitch: e.value, }); }; _startRecognizing = async () => { this.setState({ recognized: '', pitch: '', error: '', started: '', results: [], partialResults: [], end: '', }); try { await Voice.start('en-US'); } catch (e) { console.error(e); } }; _stopRecognizing = async () => { try { await Voice.stop(); } catch (e) { console.error(e); } }; _cancelRecognizing = async () => { try { await Voice.cancel(); } catch (e) { console.error(e); } }; _destroyRecognizer = async () => { try { await Voice.destroy(); } catch (e) { console.error(e); } this.setState({ recognized: '', pitch: '', error: '', started: '', results: [], partialResults: [], end: '', }); }; render() { return ( Welcome to React Native Voice! Press the button and start speaking. {`Started: ${this.state.started}`} {`Recognized: ${ this.state.recognized }`} {`Pitch: ${this.state.pitch}`} {`Error: ${this.state.error}`} Results {this.state.results.map((result, index) => { return ( {result} ); })} Partial Results {this.state.partialResults.map((result, index) => { return ( {result} ); })} {`End: ${this.state.end}`} Stop Recognizing Cancel Destroy ); } } 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 VoiceTest;