Javascript 可能未处理的承诺拒绝。自然反应
我收到以下错误:可能未处理的承诺拒绝(id:0):TypeError:null不是对象(评估“Voice.destroySpeech”) 我正在尝试将语音转换为文本,我使用了不同的库(react native voice/react native google cloud speech to text) )我也犯了同样的错误。 我希望有人能帮我解决这个问题Javascript 可能未处理的承诺拒绝。自然反应,javascript,react-native,react-native-voice,Javascript,React Native,React Native Voice,我收到以下错误:可能未处理的承诺拒绝(id:0):TypeError:null不是对象(评估“Voice.destroySpeech”) 我正在尝试将语音转换为文本,我使用了不同的库(react native voice/react native google cloud speech to text) )我也犯了同样的错误。 我希望有人能帮我解决这个问题 import React from 'react'; import { StyleSheet, Text, View,
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);
从“React”导入React;
进口{
样式表,
文本,
看法
按钮
评估学,
}从“反应本机”;
从“react National Voice”导入语音;
导出默认类VoiceNative扩展React.Component{
建造师(道具){
超级(道具);
此.state={
承认:'',
开始:“”,
结果:[],
};
Voice.onSpeechStart=this.onSpeechStart.bind(this);
Voice.onSpeechRecognized=this.onSpeechRecognized.bind(this);
Voice.onSpeechResults=this.onSpeechResults.bind(this);
}
组件将卸载(){
Voice.destroy().then(Voice.removeAllListeners);
}
onSpeechStart(e){
这是我的国家({
开始:'√',
});
};
已识别的OnSpeechRecognited(e){
这是我的国家({
认可:'√',
});
};
对等结果(e){
这是我的国家({
结果:e.value,
});
}
异步启动认知(e){
这是我的国家({
承认:'',
开始:“”,
结果:[],
});
试一试{
等待声音。开始('en-US');
}捕获(e){
控制台错误(e);
}
}
渲染(){
返回(
成绩单
{this.state.results.map((结果,索引)=>{result}
)}
);
}
}
const styles=StyleSheet.create({
发言稿:{
textAlign:'中心',
颜色:'#B0171F',
marginBottom:1,
前几名:“400%”,
},
});
AppRegistry.registerComponent('VoiceNative',()=>VoiceNative);
Voice.destroy().then(Voice.removeAllListeners.catch(e=>todo());