React native 在React Native上实现Google云语音到文本

React native 在React Native上实现Google云语音到文本,react-native,speech-to-text,google-speech-api,React Native,Speech To Text,Google Speech Api,我正在尝试在react native应用程序上实现google的语音到文本转换,但我找不到关于它的示例或文档,我对react native相当陌生,所以我有点迷路了,google官方文档上有一个链接,我试图“复制”它以react native,但我没有成功 下面是Node.js示例: async function main() { // Imports the Google Cloud client library const speech = require('@google-clou

我正在尝试在react native应用程序上实现google的语音到文本转换,但我找不到关于它的示例或文档,我对react native相当陌生,所以我有点迷路了,google官方文档上有一个链接,我试图“复制”它以react native,但我没有成功

下面是Node.js示例:

async function main() {
  // Imports the Google Cloud client library
  const speech = require('@google-cloud/speech');
  const fs = require('fs');

  // Creates a client
  const client = new speech.SpeechClient();

  // The name of the audio file to transcribe
  const fileName = './resources/audio.raw';

  // Reads a local audio file and converts it to base64
  const file = fs.readFileSync(fileName);
  const audioBytes = file.toString('base64');

  // The audio file's encoding, sample rate in hertz, and BCP-47 language code
  const audio = {
    content: audioBytes,
  };
  const config = {
    encoding: 'LINEAR16',
    sampleRateHertz: 16000,
    languageCode: 'en-US',
  };
  const request = {
    audio: audio,
    config: config,
  };

  // Detects speech in the audio file
  const [response] = await client.recognize(request);
  const transcription = response.results
    .map(result => result.alternatives[0].transcript)
    .join('\n');
  console.log(`Transcription: ${transcription}`);
}
main().catch(console.error);
首先,“fs”包不适用于react native,因此我必须使用“react native fs”,它具有不同的功能

第二,我真的应该用“require”来称呼演讲包吗?我猜react native会用“导入”来代替,对吗


关于如何在react native上实现它的任何提示?谢谢

您不能在react native上运行@google cloud/speech,因为RN在JV环境中运行,而库(@google cloud/speech)在nodejs环境中运行。您可以创建并部署api,或者使用此库

此库的上一次提交是在3年前。这也需要一个人从世博会项目中退出。如何为它创建一个api,以便它可以在JS和Expo中构建?