我正在尝试在react本机应用程序上实现google的语音转文本功能,但是我找不到关于它的示例或文档,我对react-native相当陌生,所以我有点迷失了, Google官方文档上的sample using node.js,我正尝试将其“复制”为本机,但没有成功。
这是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'
第二,我真的应该使用“要求”来调用语音包吗?我猜react-native会改用“ import”,对吧?
有关如何在react-native上实现它的任何提示?谢谢!
答案 0 :(得分:1)
您不能在react native上运行@ google-cloud / speech,因为RN在JV环境上运行,而库(@ google-cloud / speech)在nodejs环境上运行。您可以创建一个api并进行部署,也可以使用此库react-native-google-speech-api