我正在做一个项目,我需要将语音翻译成文本,我使用世博会和反应原声。**未处理的promise rejection: node_modules\react-native-voice\src\index.js:23:10 : null不是对象(计算'Voice.onSpeechStart = null') - removeAllListeners - tryCallOne中的null-tryCallOne- ... 9多个堆栈帧
import React from "react";
import { StyleSheet, Text, View, TouchableOpacity } from "react-native";
import Voice from "react-native-voice";
import * as Permissions from "expo-permissions";
export default class App extends React.Component {
constructor() {
super();
this.state = {
results: [],
};
Voice.onSpeechStart = this.onSpeechStart;
Voice.onSpeechRecognized = this.onSpeechRecognized;
Voice.onSpeechEnd = this.onSpeechEnd;
Voice.onSpeechError = this.onSpeechError;
Voice.onSpeechResults = this.onSpeechResults;
Voice.onSpeechPartialResults = this.onSpeechPartialResults;
Voice.onSpeechVolumeChanged = this.onSpeechVolumeChanged;
}
async componentDidMount() {
const {status} = await Permissions.askAsync(
Permissions.AUDIO_RECORDING
);
}
componentWillMount(){
Voice.destroy().then(Voice.removeAllListeners)
}
onSpeechStart = (e)=> {
console.log('onSpeechStart',e);
};
onSpeechRecognized =(e)=>{
console.log('onSpeechRecognized',e);
}
onSpeechEnd = (e)=>{
console.log('onSpeechEnd'+e);
}
onSpeechError =(e)=>{
console.log('onSpeechError');
}
onSpeechResults = e => {
console.log('onSpeechResults'+e);
this.setState({
results: e.value,
});
}
onSpeechPartialResults = e =>{
console.log('onSpeechPartialResults' + e.value);
}
onSpeechVolumeChanged = e =>{
console.log('onSpeechVolumeChanged');
}
_startRecognizing=async()=>{
try{
await Voice.start('en-US')
} catch(e) {
console.error(e);
}
}
_stopRecognizing = async()=>{
try{
await Voice.stop()
}catch(e){
console.error(e);
}
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity
onPress={this._startRecognizing}
style={{
backgroundColor: "green",
height: 40,
width: 100,
marginBottom: 10,
}}
>
<Text style={{
fontSize: 20,
alignSelf: "center"
}}>
Starts
</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={this._stopRecognizing}
style={{
backgroundColor: "red",
height: 40,
width: 100
}}
>
<Text style={{
fontSize: 20,
alignSelf: "center"
}}>
Stop
</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
来自框架内部**
发布于 2019-09-22 01:42:10
针对Expo 41+的更新
随着世博会SDK41的发布,添加了config plugins。react-native-voice/voice的3.2.0 release增加了对配置插件的支持。
您可以像平常一样安装依赖项
yarn add @react-native-voice/voice
然后你必须更新你的app.json
"expo": {
"plugins": [
[
"@react-native-voice/voice",
{
"microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone",
"speechRecogntionPermission": "Allow $(PRODUCT_NAME) to securely recognize user speech"
}
]
]
}你可能需要创建一个新的构建,因为你可能会得到一个不变的冲突。
现在,您应该阅读usage的文档
--
世博会40及以上
不幸的是,react-native-voice与Expo不兼容。
这样做的原因是,Expo抽象了原生iOS和安卓代码,这使得构建和开发变得快速和容易,但缺点是您不能添加需要使用原生代码的依赖项。
react-native-voice需要使用本机代码。您可以通过以下事实看出这一点:安装说明要求您链接本机代码。有关文档,请参阅here。
如果您希望在您的世博项目中使用此依赖项,则需要将其弹出。您可以在世博会文档网站here上找到有关弹出的更多信息以及这样做的优缺点
发布于 2021-10-05 08:55:57
通过世博会SDK42,您可以使用react-native-voice插件,以下是他们在文档here中提供的插件
https://stackoverflow.com/questions/58040614
复制相似问题