四零语境后端代码仓库
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.
 
 
 
 
 
 

2.4 KiB

腾讯云TTS文字转语音接口使用说明

接口地址

POST /appletApi/tts/textToVoice

请求参数

参数名称 类型 必填 描述
text String 要转换的文本内容,中文最大支持150个汉字,英文最大支持500个字母
speed Float 语速,范围:[-2,6],默认为0。-2代表0.6倍,-1代表0.8倍,0代表1.0倍,1代表1.2倍,2代表1.5倍,6代表2.5倍
voiceType Integer 音色ID,默认为0。不同音色价格有差异,完整音色列表请参见腾讯云官方文档
volume Float 音量大小,范围[-10,10],默认为0,代表正常音量
codec String 返回音频格式,可取值:wav(默认),mp3,pcm

返回结果

成功时返回二进制音频数据,Content-Type根据codec参数设置:

  • wav格式:audio/wav
  • mp3格式:audio/mpeg
  • pcm格式:audio/pcm

使用示例

基本调用

// 前端调用示例
fetch('/appletApi/tts/textToVoice', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: 'text=你好,欢迎使用语音合成服务'
})
.then(response => response.blob())
.then(blob => {
  const audio = new Audio(URL.createObjectURL(blob));
  audio.play();
});

带参数调用

// 设置语速和音色
fetch('/appletApi/tts/textToVoice', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: 'text=Hello World&speed=1.2&voiceType=1&volume=2&codec=mp3'
})
.then(response => response.blob())
.then(blob => {
  const audio = new Audio(URL.createObjectURL(blob));
  audio.play();
});

配置说明

application.yml 中配置腾讯云密钥:

tencent:
  secretId: 你的腾讯云SecretId
  secretKey: 你的腾讯云SecretKey

注意事项

  1. 需要在腾讯云控制台开通语音合成服务
  2. 确保配置的密钥有语音合成的权限
  3. 文本长度限制:中文最大150个汉字,英文最大500个字母
  4. 返回的音频数据可以直接在前端播放
  5. 不同音色和采样率可能影响合成效果和费用

错误处理

  • 如果返回204 No Content,表示合成失败或返回数据为空
  • 如果返回500 Internal Server Error,表示服务器内部错误,请检查日志
  • 请确保网络连接正常,能够访问腾讯云API服务