简体   繁体   English

如何使用客户端库在网站中使用谷歌云文本到语音?

[英]how to use google cloud text to speech in the website using client libraries?

I am developing a website where users can input the text and click a button to hear it as audio.我正在开发一个网站,用户可以在其中输入文本并单击按钮以将其作为音频收听。 I have decided to use google cloud text to speech(TTS) api.我决定使用谷歌云文本到语音(TTS)api。 All the documentation that I could find online shows how to use google TTS in a local computer by installing the google cloud library.我可以在网上找到的所有文档都显示了如何通过安装谷歌云库在本地计算机上使用谷歌 TTS。 I dont know how to get those client libraries in my website host panel and use the google api.我不知道如何在我的网站主机面板中获取这些客户端库并使用 google api。

so my question is how to use the google cloud text to speech api in the website using client libraries?所以我的问题是如何使用客户端库在网站中使用谷歌云文本到语音 api?

PS: I dont want to use the webspeech API. PS:我不想使用 webspeech API。

(too large to be a comment). (太大而不能发表评论)。

All you need to do is to recreate the documentation calls that you can see in curl on your client side.您需要做的就是重新创建您可以在客户端的 curl 中看到的文档调用。 https://cloud.google.com/text-to-speech/docs/basics https://cloud.google.com/text-to-speech/docs/basics

The reason it ask you to install libraries locally is to be able to get authentication credentials for you: (see below gcloud auth application-default print-access-token is executing a function in your computer that returns an api-key string, if your replace that with your api it will work without installing anything).它要求您在本地安装库的原因是能够为您获取身份验证凭据:(请参见下文gcloud auth application-default print-access-token正在您的计算机中执行一个返回 api-key 字符串的函数,如果您用你的api替换它,它不需要安装任何东西就可以工作)。

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) -H "Content-Type: application/json; charset=utf-8" --data "{
  'input':{
    'text':'I\'ve added the event to your calendar.'
  },
  'voice':{
    'languageCode':'en-gb',
    'name':'en-GB-Standard-A',
    'ssmlGender':'FEMALE'
  },
  'audioConfig':{
    'audioEncoding':'MP3'
  }
}" "https://texttospeech.googleapis.com/v1/text:synthesize"

In javascript this would be something like:在 javascript 中,这将类似于:

const result = await fetch('https://texttospeech.googleapis.com/v1/text:synthesize/', {
  body: '{
    "input": { "text":" ve added the event to your calendar." },
    "voice": {    
        "languageCode":"en-gb", 
        "name": "en-GB-Standard-A",
        "ssmlGender": "FEMALE" 
    },
   "audioConfig" : {"audioEncoding":"MP3"}
  }',
  headers: {
    Authorization: 'Bearer API-KEY',
    'Content-Type': 'application/json; charset=utf-8'
  },
  method: 'POST'
})

(Automatically generated with: https://kigiri.github.io/fetch/ ) (自动生成: https ://kigiri.github.io/fetch/)

The Google Speech Api is the same as any HTTP Based api, and you can communicate purely by sending HTTP/S requests over. Google Speech Api 与任何基于 HTTP 的 API 相同,您可以纯粹通过发送 HTTP/S 请求进行通信。


There might be libraries or examples only to use on the client side as well, but doing this client side is not likely to be a great decision.也可能有仅在客户端使用的库或示例,但是做这个客户端可能不是一个好决定。

The reason is that to communicate against the google speech api you will need to send your credentials to the API and by doing this on the client side you will be basically leaking your credentials to everyone in the world.原因是要与谷歌语音 api 进行通信,您需要将您的凭据发送到 API,并且通过在客户端执行此操作,您基本上会将您的凭据泄露给世界上的每个人。

To avoid that you need to set up a proxy / communicate with your own backend that then communicates with the speech api...为避免这种情况,您需要设置代理/与您自己的后端通信,然后与语音 api 通信...

Even so, You may benefit to have a backend to save already generated speeches to avoid having to convert the same countless times, saving a lot in costs.即便如此,您可能会受益于拥有一个后端来保存已经生成的语音,以避免不得不无数次转换相同的语音,从而节省大量成本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用本机 JavaScript 向 Google Cloud 文本转语音 API 进行身份验证 - Authenticate to Google Cloud text-to-speech API using native JavaScript 使用node.js谷歌云语音文本,如何获取当前工作的状态? - Using the node.js google cloud speech to text, how can I get the status of a current job? 显示Google Cloud语音转文字 - Displaying Google Cloud Speech-to-Text 如何使用谷歌将文本翻译为xpages中的语音api - how to use google translate text to speech api in xpages 使用 Javascript 的 Google Cloud Speech - Google Cloud Speech with Javascript 如何在 Cloud Functions 上恢复 Google Cloud Speech API (longRunningRecognize) 超时 - How to resume Google Cloud Speech API (longRunningRecognize) timeout on Cloud Functions 在 Javascript 中使用 Google 文字转语音 - Using Google Text-To-Speech in Javascript 如何将OAUTH与gapi客户端库同时用于Google Cloud Endpoints和Google Cloud Storage - How to use OAUTH with gapi client library for Google Cloud Endpoints and Google Cloud Storage at the same time 您如何使用客户端 JS 基于现有文本从 Google 的 Cloud Natural Language API 获取 JSON? - How do you fetch JSON from Google's Cloud Natural Language API based on existing text using client-side JS? 如何将谷歌云语音api集成到我的网页 - how to integrate google cloud speech api to my webpage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM