3

我已经创建了语音到文本转换器的应用程序。react前端和nodejs API.i从react录制音频并将其发布到nodejs。但是google API结果为空。我该如何解决?

为什么总是得到空的结果?

那是我的代码。

ReactMic 录音机

<ReactMic
    record={record}
    className="sound-wave"
    onStop={onStop}
    onData={onData}
    strokeColor="#000000"
    backgroundColor="#FF4081"
    mimeType="audio/wav"/>
<button onClick={startRecording} type="button">Start</button>
<button onClick={stopRecording} type="button">Stop</button>

NodeJs API

app.post('/SpeechConvert', (req, res) => {

    const client = new speech.SpeechClient();

    console.log(req.files.file);

    req.files.file.mv('./input.wav',function (err) {
        if (err) {
            console.log(err);
        }
    })



    async function speechToText() {

        // The name of the audio file to transcribe
        const fileData = req.files.file.data;

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

        // console.log(audioBytes);
        // The audio file's encoding, sample rate in hertz, and BCP-47 language code
        const audio = {
            content: audioBytes,
        };
        const config = {
            enableAutomaticPunctuation: true,
            encoding: 'LINEAR16',
            sampleRateHertz: 44100,
            languageCode: 'en-US',
        };
        const request = {
            audio: audio,
            config: config,
        };

        // Detects speech in the audio file
        const [response] = await client.recognize(request);
        console.log(response);
        const transcription = response.results
            .map(result => result.alternatives[0].transcript)
            .join('\n');
        console.log(`Transcription: ${transcription}`);
        res.send({ 'transcription': transcription, 'msg': 'The Audio successfully converted to the text' });
    }

    speechToText().catch(console.error);

});

谁能帮我解决这个问题?

4

0 回答 0