语音交互服务 SIS-语音合成:请求示例

时间:2025-01-26 10:45:25

请求示例

“endpoint”即调用API的请求地址,不同服务不同区域的“endpoint”不同,具体请参见终端节点

  • 调用 语音合成 API,将文本合称为语音,并调整语音的音色、语速、音高、音量
    POST https://{endpoint}/v1/{project_id}/ttsRequest Header:Content-Type: application/json X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...      Request Body: {   "text": "欢迎使用语音云服务。",   "config":    {      "audio_format": "wav",      "sample_rate": "8000",      "property": "chinese_xiaoyan_common",     "speed": 10,     "pitch": 10,     "volume": 60   } } 
  • 使用Python3语言调用语音合成API,将文本合称为语音,并调整语音的音色、语速、音高、音量
    # -*- coding: utf-8 -*-# 此demo仅供测试使用,强烈建议使用sdk。需提前安装requests,执行pip install requestsimport requestsimport jsondef stts_demo():    url = 'https://{{endpoint}}/v1/{{project_id}}/tts'  # endpoint和project_id需替换    token = '用户对应region的token'    text = '待识别的文本'    header = {        'Content-Type': 'application/json',        'X-Auth-Token': token    }    body = {'text': text}    resp = requests.post(url, data=json.dumps(body), headers=header)    print(resp.text)if __name__ == '__main__':    stts_demo()
  • 使用Java语言调用语音合成API,将文本合称为语音,并调整语音的音色、语速、音高、音量
    import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;/** * 此demo仅供测试使用,强烈建议使用SDK */public class SttsDemo {  public void sttsDemo() {    try {      // endpoint和projectId需要替换成实际信息。      URL url = new URL("https://{{endpoint}}/v1/{{project_id}}/tts");      String token = "对应region的token";      HttpURLConnection connection = (HttpURLConnection)url.openConnection();      connection.setRequestMethod("POST");      connection.setDoInput(true);      connection.setDoOutput(true);      connection.addRequestProperty("Content-Type", "application/json");      connection.addRequestProperty("X-Auth-Token", token);      OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");      String body = "{\"text\": \"123\"}";      osw.append(body);      osw.flush();      InputStream is = connection.getInputStream();      BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));      while (br.ready()) {        System.out.println(br.readLine());      }    } catch (Exception e) {      e.printStackTrace();    }  }  public static void main(String[] args) {    SttsDemo sttsDemo = new SttsDemo();    sttsDemo.sttsDemo();  }}
support.huaweicloud.com/api-sis/sis_03_0111.html