如何解决可能未处理的承诺拒绝反应原生
我收到以下错误:可能未处理的承诺拒绝 (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: '',});
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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。