代码拉取完成,页面将自动刷新
同步操作将从 greya/go语言自实现协程池 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
type Speak struct {
XMLName xml.Name `xml:"speak"`
Version string `xml:"version,attr"`
Lang string `xml:"xml:lang,attr"`
Voice []Voice `xml:"Voice"`
}
type Voice struct {
XMLName xml.Name `xml:"voice"`
Name string `xml:"name,attr"`
Lang string `xml:"xml:lang,attr"`
InnerText string `xml:",innerxml"`
}
var url = "https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken"
var baseUrl = "https://westus.tts.speech.microsoft.com/"
var subscription_key = "fc700fc2c8914dc796720f2a4f9553a6"
var input = "A US senior official recently “warned” the UK on its approval to let Chinese telecoms company Huawei to help build some non-core parts of 5G data network, saying that it could allow China to “control the internet of the future” and “divide Western alliances through bits and bytes”."
var timeTemple = "20190513-0000"
var path = "cognitiveservices/v1"
func main() {
client := &http.Client{}
//提交请求
reqest, err := http.NewRequest("POST", url, nil)
//增加header选项
reqest.Header.Add("Ocp-Apim-Subscription-Key", subscription_key)
if err != nil {
panic(err)
}
//处理返回结果
response, _ := client.Do(reqest)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(body))
accessToken := string(body)
constructedUrl := baseUrl + path
speak := Speak{Version: "1.0", Lang: "en-us"}
speak.Voice = append(speak.Voice, Voice{Name: "en-US-Guy24kRUS", Lang: "en-us", InnerText: input})
data, _ := xml.MarshalIndent(&speak, "", " ")
bodyStr := string(data)
//read, w := io.Pipe()
//defer read.Close()
//defer w.Close()
//_, err = read.Read([]byte(bodyStr))
reqest, err = http.NewRequest("POST", constructedUrl, strings.NewReader(bodyStr))
//增加header选项
reqest.Header.Add("Authorization", "Bearer "+accessToken)
reqest.Header.Add("Content-Type", "application/ssml+xml")
reqest.Header.Add("X-Microsoft-OutputFormat", "riff-24khz-16bit-mono-pcm")
reqest.Header.Add("User-Agent", "YOUR_RESOURCE_NAME")
if err != nil {
panic(err)
}
//处理返回结果
response, _ = client.Do(reqest)
defer response.Body.Close()
if response.StatusCode == 200 {
body, err = ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
content := []byte(body)
//将指定内容写入到文件中
nameStr := "sample-" + time.Now().Format(timeTemple) + ".wav"
err := ioutil.WriteFile(nameStr, content, 0666)
if err != nil {
fmt.Println("ioutil WriteFile error: ", err)
}
} else {
fmt.Println("\nStatus code: ", string(response.StatusCode), "\nSomething went wrong. Check your subscription key and headers.\n")
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。