javascript - 可能未处理的承诺拒绝。反应原生
问题描述
我收到以下错误:可能未处理的承诺拒绝(id:0):TypeError:null 不是对象(评估'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);
解决方案
推荐阅读
- c# - 在 C# 中从字典创建一个随机列表,包括一个特定的选定项
- javascript - 我正在尝试根据使用 forEach 的另一个 API 请求的结果发出 API 请求。我如何在不使用 forEach 的情况下存档相同的结果
- matlab - 使用 fmincon() 求解矩阵形式的非线性程序
- html - 浏览器如何解释非标准/自定义 html 标签
- javascript - 按字母顺序搜索在 DataTable 中不起作用
- python - 将 Pandas 数据框传递给 python 中的函数
- angular - 如何从路由器插座中的另一个组件更改角度组件的不透明度?
- python - 计算shell脚本中矩阵中沿行和列但不是对角线的连接非零的数量
- flutter - 我们可以在手机上通过 chrome 浏览器打开 Flutter Web 应用吗?
- react-native - VirtualizedLists 不应该嵌套在具有相同方向的普通 ScrollViews 中 - 改用另一个 VirtualizedList-backed 容器