0

我正在做一个项目,我需要将语音翻译成我使用 Expo 和 React-native-voice 的文本。**[未处理的承诺拒绝:TypeError:null 不是对象(评估'Voice.onSpeechStart = null')] - removeAllListeners 中的 node_modules\react-native-voice\src\index.js:23:10 - node_modules\promise\ setimmediate\core.js:37:14 in 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",
  },
});

来自框架内部**

4

2 回答 2

2

41+ 世博会更新

随着 Expo SDK 41 的发布,添加了配置插件3.2.0 版本增加react-native-voice/voice了对配置插件的支持。

您像往常一样安装依赖项

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"
      }
    ]
  ]
}

您可能需要创建一个新构建,因为您可能会遇到不变的违规行为。

您现在应该阅读使用文档

——

世博会 40 岁及以上

不幸react-native-voice的是与世博不兼容。

这样做的原因是 Expo 将原生 iOS 和 Android 代码从你身上抽象出来,这使得构建和开发变得快速和容易,但缺点是你不能添加需要你使用原生代码的依赖项。

react-native-voice需要使用本机代码。您可以通过安装说明要求您链接本机代码这一事实来看到这一点。有关文档,请参见此处

如果您希望在您的 Expo 项目中使用此依赖项,则需要将其弹出。您可以在此处的 Expo 文档网站上找到有关弹出的更多信息以及这样做的利弊

于 2019-09-21T17:42:10.217 回答
1

使用 Expo SDK42,您可以使用该react-native-voice插件,这是他们在此处的文档中的内容

于 2021-10-05T08:55:57.290 回答