问题描述
我收到以下错误:可能未处理的承诺拒绝 (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 (将#修改为@)