【问题标题】:send google cloud text-to-speech audio file from node server to a react application将谷歌云文本到语音的音频文件从节点服务器发送到反应应用程序
【发布时间】:2022-08-08 15:41:57
【问题描述】:

我对从 Nodejs 服务器向我的应用程序的反应前端发送音频文件有一些疑问。我有几个问题,

  1. 在将 mp3 文件发送到前端之前,是否必须将其保存在本地?
  2. 将音频文件发送到前端的最佳方式是什么? (流/作为文件发送/任何建议)
  3. 是否有任何服务在发送字符串时将 URL 发送回转换后的 mp3 文件。

    到目前为止,在本地转换和保存音频文件没有问题。我想要将音频文件发送到前端的最方便的选项。提前致谢。

  • 您可以查看this 链接。

标签: node.js reactjs google-text-to-speech


【解决方案1】:

您不需要将 mp3 文件本地存储在服务器中,因为您从第三个服务获取音频流。 所以你需要做的就是将流传递回你的客户端(前端),做这样的事情(假设你使用 express):

import textToSpeech from '@google-cloud/text-to-speech'
import { PassThrough } from 'stream'

const client = new textToSpeech.TextToSpeechClient()

export default class AudioController {
  static async apiGetPronounce(req, res, next) {
    try {
      const request = {
        input: { text: req.query.text },
        voice: { languageCode: req.query.langCode, ssmlGender: 'NEUTRAL' },
        audioConfig: { audioEncoding: 'MP3' },
      }

      res.set({
        'Content-Type': 'audio/mpeg',
        'Transfer-Encoding': 'chunked'
      })

      const [response] = await client.synthesizeSpeech(request)
      const bufferStream = new PassThrough()
      bufferStream.end(Buffer.from(response.audioContent))
      bufferStream.pipe(res)
    } catch (e) {
      console.log(`api, ${e}`)
      res.status(500).json({ error: e })
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多