首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在做一个项目,我需要将语音翻译成文本,我使用世博会和反应原生声音

我正在做一个项目,我需要将语音翻译成文本,我使用世博会和反应原生声音
EN

Stack Overflow用户
提问于 2019-09-21 21:09:23
回答 2查看 1.4K关注 0票数 0

我正在做一个项目,我需要将语音翻译成文本,我使用世博会和反应原声。**未处理的promise rejection: node_modules\react-native-voice\src\index.js:23:10 : null不是对象(计算'Voice.onSpeechStart = null') - removeAllListeners - tryCallOne中的null-tryCallOne- ... 9多个堆栈帧

代码语言:javascript
复制
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",
  },
});

来自框架内部**

EN

回答 2

Stack Overflow用户

发布于 2019-09-22 01:42:10

针对Expo 41+的更新

随着世博会SDK41的发布,添加了config pluginsreact-native-voice/voice3.2.0 release增加了对配置插件的支持。

您可以像平常一样安装依赖项

yarn add @react-native-voice/voice

然后你必须更新你的app.json

代码语言:javascript
复制
"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上找到有关弹出的更多信息以及这样做的优缺点

票数 2
EN

Stack Overflow用户

发布于 2021-10-05 08:55:57

通过世博会SDK42,您可以使用react-native-voice插件,以下是他们在文档here中提供的插件

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58040614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档