react-native-voice - 语音转文本在 android 中不起作用

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

我正在使用 react-native-voice 和 expo-speech 库来转录我的声音并将文本转换为语音。问题是,仅在 android 中,当我启动代码时运行 Voice.onSpeechStarts 然后它返回 false 并且不记录任何内容。但在 iOS 中它运行良好。我也在使用 expo speech 在录制语音后立即说出一些组件。

package.json 版本:

"react": "18.2.0",
"react-native": "0.71.0",
"@react-native-voice/voice": "^3.2.4",

Note: I have tried with voice versions : 3.1.5, 3.2.4
Android sdk version : 31

代码:

export const Screen = () => {
  const [isRecording, setIsRecording] = useState(false);
  const [userMessage, setUserMessage] = useState('');

  useEffect(() => {
    Voice.onSpeechStart = onSpeechStartHandler;
    Voice.onSpeechEnd = onSpeechEndHandler;
    Voice.onSpeechResults = onSpeechResultsHandler;
    return () => {
      Voice.destroy().then(Voice.removeAllListeners);
    };
  }, []);

  const onSpeechStartHandler = e => {
    console.log('start handler=»', e);
  };

  const onSpeechEndHandler = e => {
    console.log('stop handler', e);
  };
  const onSpeechResultsHandler = e => {
    console.log('speech result handler', e);
    setUserMessage(e.value[0]);
  };

  const startRecording = async () => {
    setIsRecording(true);
    try {
      await Voice.start('en-US');
    } catch (e) {
      console.log('error -> ', e);
    }
  };

  const stopRecording = () => {
    setIsRecording(false);
    try {
      Voice.stop();
      console.log(userMessage);
    } catch (e) {
      console.log('error -> ', e);
    }
  };

  return (
    <View
      style={{
        alignContent: 'center',
        justifyContent: 'center',
        backgroundColor: 'black',
        flex: 1,
      }}>
      <Button
        title={isRecording ? 'Stop Speaking' : 'Start Speaking'}
        onPress={isRecording ? stopRecording : startRecording}
      />
    </View>
  );
};

Error after running the code

When I try to check available speech services

谢谢你的时间。

android react-native speech-to-text react-native-voice
2个回答
0
投票

react native voice/voice 版本不兼容android 12以上的版本,根据GitHub中提到的open issue, 请参考这个https://github.com/react-native-voice/voice/issues/429 要么你需要在库(android)的本机端进行更改,要么尝试降级你的 android 版本,你可能需要等待更新...... 希望对你有帮助


0
投票

在我的例子中,我做了这个解决方案: node_modules-> react-native-voice/voice -> android -> 打开 androidManifest 并添加这个块

<queries>
        <intent>
            <action android:name="android.speech.RecognitionService" />
        </intent>
    </queries>

我在 Android 13 设备上测试过,试试这个✌️

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