我正试图在react native应用程序上实现谷歌的语音到文本,但我找不到关于它的示例或文档,我对react native还很陌生,所以我有点迷路了,谷歌官方文档上有一个使用node.js的示例,我正试图将其"复制"为react natural,但我没有成功。
下面是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 nativefs",它具有不同的功能
第二,我真的应该用"要求"来调用演讲包吗?我猜react native会使用"import",对吧?
关于如何在react native上实现它,有什么提示吗?谢谢
由于RN在JV环境上运行,库(@google cloud/speech)在nodejs环境下运行,因此您无法在react native上运行@google cloud/sispeech。你可以创建一个api并部署它,或者使用这个库react原生谷歌语音api