React Native 将本机语音转换为文本- GoogleCloudSpeechToText

ryoqjall  于 2022-11-30  发布在  React
关注(0)|答案(1)|浏览(294)

我是react native的新手,正在尝试在我的应用程序中开发语音转录功能。我希望在人们使用麦克风时得到一个字符串。我在网上找到了几个解决方案,但对于native应用程序,我使用的是EXPO。我使用的是“react-native-google-cloud-speech-to-text”,但我一直收到以下错误:类型错误:null不是对象(计算“GoogleCloudSpeechToText. setApiKey”)
你曾经面对过这个问题吗?

export default function Notes() {
  const [transcript, setResult] = useState("");
  const [fileId, setId] = useState("");
  const [file, setFile] = useState("");
  
  const API_KEY = '**************************';

  useEffect(() => {
    PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, {
      title: "Cool Photo App Camera Permission",
      message:
        "Cool Photo App needs access to your camera " +
        "so you can take awesome pictures.",
      buttonNeutral: "Ask Me Later",
      buttonNegative: "Cancel",
      buttonPositive: "OK",
    });
  }, []);

  useEffect(() => {
    GoogleCloudSpeechToText.setApiKey(API_KEY);
    GoogleCloudSpeechToText.onVoice(onVoice);
    GoogleCloudSpeechToText.onVoiceStart(onVoiceStart);
    GoogleCloudSpeechToText.onVoiceEnd(onVoiceEnd);
    GoogleCloudSpeechToText.onSpeechError(onSpeechError);
    GoogleCloudSpeechToText.onSpeechRecognized(onSpeechRecognized);
    GoogleCloudSpeechToText.onSpeechRecognizing(onSpeechRecognizing);
    return () => {
      GoogleCloudSpeechToText.removeListeners();
    };
  }, []);

  const onSpeechError = (_error) => {
    console.log("onSpeechError: ", _error);
  };

  const onSpeechRecognized = (result) => {
    console.log("onSpeechRecognized: ", result);
    setResult(result.transcript);
  };

  const onSpeechRecognizing = (result) => {
    console.log("onSpeechRecognizing: ", result);
    setResult(result.transcript);
  };

  const onVoiceStart = (_event) => {
    console.log("onVoiceStart", _event);
  };

  const onVoice = (_event) => {
    console.log("onVoice", _event);
  };

  const onVoiceEnd = () => {
    console.log("onVoiceEnd: ");
  };

  const startRecognizing = async () => {
    const result = await GoogleCloudSpeechToText.start({
      speechToFile: true,
    });
    console.log("startRecognizing", result);
    if (result.fileId) setId(result.fileId);
  };

  const stopRecognizing = async () => {
    await GoogleCloudSpeechToText.stop();
  };

  const getAudioFile = async () => {
    if (fileId) {
      const result = await GoogleCloudSpeechToText.getAudioFile(fileId, {
        channel: 2,
        bitrate: 96000,
        sampleRate: 44100,
      });
      console.log(result);
      if (result.path) {
        setFile(result.path);
      }
    }
  };


  return (
    <SafeAreaView style={styles.container}>
      <View>
        <Text style={styles.title}>{transcript}</Text>
        <Button title="Start me" onPress={startRecognizing} />
      </View>
      <Separator />
      <View>
        <Text style={styles.title}>
          Adjust the color in a way that looks standard on each platform. On
          iOS, the color prop controls the color of the text. On Android, the
          color adjusts the background color of the button.
        </Text>
        <Button title="Stop me" color="#f194ff" onPress={stopRecognizing} />
      </View>
      <Separator />
      <View>
        <Text style={styles.title}>{file}</Text>
        <Button
          disabled={transcript === ""}
          title="Get file audio"
          color="#f194ff"
          onPress={getAudioFile}
        />
      </View>
      {/*{file && (*/}
      {/*  <>*/}
      {/*    <Separator />*/}
      {/*    <Text style={styles.title}>file</Text>*/}
      {/*    <View>*/}
      {/*      <Button*/}
      {/*        disabled={transcript === ''}*/}
      {/*        title="Play"*/}
      {/*        color="#f194ff"*/}
      {/*        onPress={playAudio}*/}
      {/*      />*/}
      {/*    </View>*/}
      {/*  </>*/}
      {/*)}*/}
    </SafeAreaView>
  );
}
v9tzhpje

v9tzhpje1#

我没有遇到过这个问题,但在StackOverflow中单击“运行代码段”时,我看到了一个不同的错误:

Error: {
  "message": "Uncaught SyntaxError: Unexpected token 'export'",
  "filename": "https://stacksnippets.net/js",
  "lineno": 12,
  "colno": 9
}

也许堆栈跟踪会有所帮助?

相关问题