使用react-native-community/voice在React Native中进行语音到文本

问题描述 投票:0回答:2

我想将语音转换为文本,并借助语音识别获取文本结果。

我正在使用 react-native-community/voice示例

构建项目并在手机上安装

apk
并按录制按钮后,我收到以下错误:

错误:{“消息”:“5/客户端错误”}

注意:我在AndroidManifest中添加了以下权限:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

这是我的代码():

import React, { Component, useState, useEffect } from 'react';
import { StyleSheet, Text, View, Image, TouchableHighlight } from 'react-native';

import Voice from '@react-native-community/voice';

const App = (props) => {
  const [voiceState, setVoiceState] = useState({
    recognized: '',
    pitch: '',
    error: '',
    end: '',
    started: '',
    results: [],
    partialResults: [],
  })

  useEffect(() => {
    Voice.onSpeechStart = onSpeechStart;
    Voice.onSpeechRecognized = onSpeechRecognized;
    Voice.onSpeechEnd = onSpeechEnd;
    Voice.onSpeechError = onSpeechError;
    Voice.onSpeechResults = onSpeechResults;
    Voice.onSpeechPartialResults = onSpeechPartialResults;
    Voice.onSpeechVolumeChanged = onSpeechVolumeChanged;

    Voice.destroy().then(Voice.removeAllListeners);
  }, [])


  const onSpeechStart = (e) => {
    console.log('onSpeechStart: ', e);
    setVoiceState({ ...voiceState, started: '√', })
  };

  const onSpeechRecognized = (e) => {
    console.log('onSpeechRecognized: ', e);
    setVoiceState({
      ...voiceState, recognized: '√',
    })
  };

  const onSpeechEnd = (e) => {
    console.log('onSpeechEnd: ', e);
    setVoiceState({
      ...voiceState, end: '√',
    })
  };

  const onSpeechError = (e) => {
    console.log('onSpeechError: ', e);
    setVoiceState({
      ...voiceState, error: JSON.stringify(e.error)
    })
  };

  const onSpeechResults = (e) => {
    console.log('onSpeechResults: ', e);
    setVoiceState({
      ...voiceState, results: e.value,
    })
  };

  const onSpeechPartialResults = (e) => {
    console.log('onSpeechPartialResults: ', e);
    setVoiceState({
      ...voiceState, partialResults: e.value,
    })
  };

  const onSpeechVolumeChanged = (e) => {
    console.log('onSpeechVolumeChanged: ', e);
    setVoiceState({
      ...voiceState, pitch: e.value,
    })
  };

  const _startRecognizing = async () => {
    setVoiceState({
      ...voiceState,
      recognized: '',
      pitch: '',
      error: '',
      started: '',
      results: [],
      partialResults: [],
      end: '',
    })
    try {
      await Voice.start('en-US');
    } catch (e) {
      console.error(e);
    }
  };

  const _stopRecognizing = async () => {
    try {
      await Voice.stop();
    } catch (e) {
      console.error(e);
    }
  };

  const _cancelRecognizing = async () => {
    try {
      await Voice.cancel();
    } catch (e) {
      console.error(e);
    }
  };

  const _destroyRecognizer = async () => {
    try {
      await Voice.destroy();
    } catch (e) {
      console.error(e);
    }
    setVoiceState({
      ...voiceState,
      recognized: '',
      pitch: '',
      error: '',
      started: '',
      results: [],
      partialResults: [],
      end: '',
    })
  };

  return (
    <View style={styles.container}>
      <Text style={styles.welcome}>Welcome to React Native Voice!</Text>
      <Text style={styles.instructions}>
        Press the button and start speaking.
    </Text>
      <Text style={styles.stat}>{`Started: ${voiceState.started}`}</Text>
      <Text style={styles.stat}>{`Recognized: ${
        voiceState.recognized
        }`}</Text>
      <Text style={styles.stat}>{`Pitch: ${voiceState.pitch}`}</Text>
      <Text style={styles.stat}>{`Error: ${voiceState.error}`}</Text>
      <Text style={styles.stat}>Results</Text>
      {voiceState.results.map((result, index) => {
        return (
          <Text key={`result-${index}`} style={styles.stat}>
            {result}
          </Text>
        );
      })}
      <Text style={styles.stat}>Partial Results</Text>
      {voiceState.partialResults.map((result, index) => {
        return (
          <Text key={`partial-result-${index}`} style={styles.stat}>
            {result}
          </Text>
        );
      })}
      <Text style={styles.stat}>{`End: ${voiceState.end}`}</Text>
      <TouchableHighlight onPress={_startRecognizing}>
        <Image style={styles.button} source={require('../assets/voice-recording.png')} />
      </TouchableHighlight>
      <TouchableHighlight onPress={_stopRecognizing}>
        <Text style={styles.action}>Stop Recognizing</Text>
      </TouchableHighlight>
      <TouchableHighlight onPress={_cancelRecognizing}>
        <Text style={styles.action}>Cancel</Text>
      </TouchableHighlight>
      <TouchableHighlight onPress={_destroyRecognizer}>
        <Text style={styles.action}>Destroy</Text>
      </TouchableHighlight>
    </View>
  );
}
react-native speech-to-text
2个回答
0
投票

我之前在 Android 模拟器上见过这种情况,但在真实设备上并没有发生。您使用的模拟器/设备可能不支持语音识别。

在启动侦听器之前,您应该使用库中的 isAvailable 方法来确保设备能够处理语音识别。


0
投票

嗯,我也不太明白这里的问题。您是否尝试过使用其他语音到文本转换器?可能这就是问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.