我收到以下错误:可能未处理的承诺拒绝 (id:0): TypeError: null is not an object (evaluating 'Voice.destroySpeech')'
我正在尝试将语音转换为文本,我使用了不同的库(react-native-voice / react-native-google-cloud-speech-to-text ),我也遇到了同样的错误。 希望有人能帮我解决这个问题
import React from 'react';
import {
StyleSheet,
Text,
View,
Button,
AppRegistry,
} from 'react-native';
import Voice from 'react-native-voice';
export default class VoiceNative extends React.Component {
constructor(props) {
super(props);
this.state = {
recognized: '',
started: '',
results: [],
};
Voice.onSpeechStart = this.onSpeechStart.bind(this);
Voice.onSpeechRecognized = this.onSpeechRecognized.bind(this);
Voice.onSpeechResults = this.onSpeechResults.bind(this);
}
componentWillUnmount() {
Voice.destroy().then(Voice.removeAllListeners);
}
onSpeechStart(e) {
this.setState({
started: '√',
});
};
onSpeechRecognized(e) {
this.setState({
recognized: '√',
});
};
onSpeechResults(e) {
this.setState({
results: e.value,
});
}
async _startRecognition(e) {
this.setState({
recognized: '',
started: '',
results: [],
});
try {
await Voice.start('en-US');
} catch (e) {
console.error(e);
}
}
render () {
return (
<View>
<Text style={styles.transcript}>
Transcript
</Text>
{this.state.results.map((result, index) => <Text style={styles.transcript}> {result}</Text>
)}
<Button style={styles.transcript}
onPress={this._startRecognition.bind(this)}
title="Start"></Button>
</View>
);
}
}
const styles = StyleSheet.create({
transcript: {
textAlign: 'center',
color: '#B0171F',
marginBottom: 1,
top: '400%',
},
});
AppRegistry.registerComponent('VoiceNative', () => VoiceNative);