您现在的位置是:网站首页> Go语言
Go调用阿里长语音合成例子
- Go语言
- 2021-05-07
- 795人已阅读
package main
import (
"fmt"
"net/http"
"io/ioutil"
"encoding/json"
"strconv"
"bytes"
"time"
)
// 长文本语音合成restful接口,支持post调用,不支持get请求。发出请求后,可以轮询状态或者等待服务端合成后自动回调(如果设置了回调参数)。
func requestLongTts4Post(text string, appkey string, token string) {
// var url string= "http://pre-nls-gateway.aliyuncs.com:80/rest/v1/tts/async"
var url string= "https://nls-gateway.cn-shanghai.aliyuncs.com/rest/v1/tts/async"
// 拼接HTTP Post请求的消息体内容。
context := make(map[string]interface{})
context["device_id"] = "test-device"
header := make(map[string]interface{})
header["appkey"] = appkey
header["token"] = token
tts_request := make(map[string]interface{})
tts_request["text"] = text
tts_request["format"] = "wav"
tts_request["voice"] = "xiaogang"
tts_request["sample_rate"] = 16000
tts_request["enable_subtitle"] = false
payload := make(map[string]interface{})
payload["enable_notify"] = false
payload["notify_url"] = "http://123.com"
payload["tts_request"] = tts_request
ttsBody := make(map[string]interface{})
ttsBody["context"] = context
ttsBody["header"] = header
ttsBody["payload"] = payload
ttsBodyJson, err := json.Marshal(ttsBody)
if err != nil {
panic(nil)
}
fmt.Println(string(ttsBodyJson))
// 发送HTTPS POST请求,处理服务端的响应。
response, err := http.Post(url, "application/json;charset=utf-8", bytes.NewBuffer([]byte(ttsBodyJson)))
if err != nil {
panic(err)
}
defer response.Body.Close()
contentType := response.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(contentType))
fmt.Println(string(body))
statusCode := response.StatusCode
if(statusCode == 200) {
fmt.Println("The POST request succeed!")
var f interface{}
err := json.Unmarshal(body, &f)
if err != nil {
fmt.Println(err)
}
// 从消息体中解析出来task_id(重要)和request_id。
var taskId string = ""
var requestId string = ""
m := f.(map[string]interface{})
for k, v := range m {
if(k == "error_code") {
fmt.Println("error_code = ", v)
} else if(k == "request_id") {
fmt.Println("request_id = ", v)
requestId = v.(string)
} else if(k == "data") {
fmt.Println("data = ", v)
data := v.(map[string]interface{})
fmt.Println(data)
taskId = data["task_id"].(string)
}
}
// 说明:轮询检查服务端的合成状态,轮询操作非必须,如果设置了回调url,则服务端会在合成完成后主动回调。
waitLoop4Complete(url, appkey, token, taskId, requestId)
} else {
fmt.Println("The POST request failed: " + string(body))
fmt.Println("The HTTP statusCode: " + strconv.Itoa(statusCode))
}
}
// 根据特定信息轮询检查某个请求在服务端的合成状态,轮询操作非必须,如果设置了回调url,则服务端会在合成完成后主动回调。
func waitLoop4Complete(url string, appkey string, token string, task_id string, request_id string) {
var fullUrl string = url + "?appkey=" + appkey + "&task_id=" + task_id + "&token=" + token + "&request_id=" + request_id
fmt.Println("fullUrl=" + fullUrl)
for {
response, err := http.Get(fullUrl)
if err != nil {
fmt.Println("The GET request failed!")
panic(err)
}
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("waitLoop4Complete = ", string(body))
var f interface{}
json.Unmarshal(body, &f)
if err != nil {
fmt.Println(err)
}
// 从消息体中解析出来task_id(重要)和request_id。
var code float64 = 0
var taskId string = ""
var audioAddress string = ""
m := f.(map[string]interface{})
for k, v := range m {
if(k == "error_code") {
code = v.(float64)
} else if(k == "request_id") {
} else if(k == "data") {
if (v != nil) {
data := v.(map[string]interface{})
taskId = data["task_id"].(string)
audioAddress = data["audio_address"].(string)
}
}
}
if (code == 20000000 && audioAddress != "") {
fmt.Println("Tts Finished! task_id = " + taskId);
fmt.Println("Tts Finished! audio_address = " + audioAddress);
break
} else {
time.Sleep(time.Duration(3)*time.Second)
}
}
}
func main() {
var appkey string = "yourAppkey"
var token string = "yourToken"
var text string = "今天是周一,天气挺好的。"
requestLongTts4Post(text, appkey, token)
}