React Native中的Google Speech To Text API

时间:2017-08-03 15:16:49

标签: react-native speech-to-text

我正在使用React Native做一个具有以下功能的项目 1.用户开始录制音频 2.停止录制音频 3.保存音频 4.将其翻译成文本(在我的情况下翻译成越南语)

我完成了1,2,3。但是我坚持4,我不知道如何使用带有音频文件输入的Google STT API,因为它似乎只是使用STT作为意图。

希望任何想法或解决方案相关!

这是我的代码:

requestAPI() {
// Imports the Google Cloud client library
const Speech = require('@google-cloud/speech')({
  projectId: 'speech-to-text-175801',
  keyFilename: '/keyfile.json'
});

const RNFS = require('react-native-fs');

// Your Google Cloud Platform project ID
const projectId = 'speech-to-text-175801';

// Instantiates a client
const speechClient = Speech({
  projectId: projectId,
  keyFilename: '/keyfile.json'
});

// The name of the audio file to transcribe
const fileName = this.state.audioPath;

// Reads a local audio file and converts it to base64
const file = RNFS.readFile(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: 'aac',
  sampleRateHertz: 32000,
  languageCode: 'vi-VN'
};

const request = {
  audio: audio,
  config: config
};

// Detects speech in the audio file
speechClient.recognize(request)
  .then((results) => {
    const transcription = results[0].results[0].alternatives[0].transcript;
    this.setState({
      textReceived: transcription
    })
  })
  .catch((err) => {
    this.setState({
      textReceived: 'Có lỗi Google Cloud STT, mời bạn request lại'
    })
  });

}

现在出错:'无法解析模块" child_process"来自... \ google-auth-library \ lib \ auth \ googleauth.js"

1 个答案:

答案 0 :(得分:0)

我认为问题是你不能在 react native 上使用 Google 语音 SDK,它是一个节点库。所以你需要先将你的文件发送到 nodejs 服务器,然后调用 sdk 方法。