四零语境后端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.4 KiB

1 day ago
  1. # 腾讯云TTS文字转语音接口使用说明
  2. ## 接口地址
  3. ```
  4. POST /appletApi/tts/textToVoice
  5. ```
  6. ## 请求参数
  7. | 参数名称 | 类型 | 必填 | 描述 |
  8. |---------|------|------|------|
  9. | text | String | 是 | 要转换的文本内容,中文最大支持150个汉字,英文最大支持500个字母 |
  10. | speed | Float | 否 | 语速,范围:[-2,6],默认为0。-2代表0.6倍,-1代表0.8倍,0代表1.0倍,1代表1.2倍,2代表1.5倍,6代表2.5倍 |
  11. | voiceType | Integer | 否 | 音色ID,默认为0。不同音色价格有差异,完整音色列表请参见腾讯云官方文档 |
  12. | volume | Float | 否 | 音量大小,范围[-10,10],默认为0,代表正常音量 |
  13. | codec | String | 否 | 返回音频格式,可取值:wav(默认),mp3,pcm |
  14. ## 返回结果
  15. 成功时返回二进制音频数据,Content-Type根据codec参数设置:
  16. - wav格式:audio/wav
  17. - mp3格式:audio/mpeg
  18. - pcm格式:audio/pcm
  19. ## 使用示例
  20. ### 基本调用
  21. ```javascript
  22. // 前端调用示例
  23. fetch('/appletApi/tts/textToVoice', {
  24. method: 'POST',
  25. headers: {
  26. 'Content-Type': 'application/x-www-form-urlencoded',
  27. },
  28. body: 'text=你好,欢迎使用语音合成服务'
  29. })
  30. .then(response => response.blob())
  31. .then(blob => {
  32. const audio = new Audio(URL.createObjectURL(blob));
  33. audio.play();
  34. });
  35. ```
  36. ### 带参数调用
  37. ```javascript
  38. // 设置语速和音色
  39. fetch('/appletApi/tts/textToVoice', {
  40. method: 'POST',
  41. headers: {
  42. 'Content-Type': 'application/x-www-form-urlencoded',
  43. },
  44. body: 'text=Hello World&speed=1.2&voiceType=1&volume=2&codec=mp3'
  45. })
  46. .then(response => response.blob())
  47. .then(blob => {
  48. const audio = new Audio(URL.createObjectURL(blob));
  49. audio.play();
  50. });
  51. ```
  52. ## 配置说明
  53. `application.yml` 中配置腾讯云密钥:
  54. ```yaml
  55. tencent:
  56. secretId: 你的腾讯云SecretId
  57. secretKey: 你的腾讯云SecretKey
  58. ```
  59. ## 注意事项
  60. 1. 需要在腾讯云控制台开通语音合成服务
  61. 2. 确保配置的密钥有语音合成的权限
  62. 3. 文本长度限制:中文最大150个汉字,英文最大500个字母
  63. 4. 返回的音频数据可以直接在前端播放
  64. 5. 不同音色和采样率可能影响合成效果和费用
  65. ## 错误处理
  66. - 如果返回204 No Content,表示合成失败或返回数据为空
  67. - 如果返回500 Internal Server Error,表示服务器内部错误,请检查日志
  68. - 请确保网络连接正常,能够访问腾讯云API服务