標籤:ext doc har 參考 secret empty service ddr ...
因為用得到,所以作個記錄:
代碼如下:
public class BaiduTTSService : IBaiduTTSService { public string tok = GetBaiduToken(); private const string lan = "zh";//語言 private const string per = "4";//發音人選擇 0位女 1位男 預設 女0為女聲,1為男聲,3為情感合成-度逍遙,4為情感合成-度丫丫,預設為普通女聲 private const string ctp = "1";//用戶端類型選擇 web端為1 private const string spd = "3";//範圍0~9 預設 5 語速 private const string pit = "4";//範圍0~9 預設 5 音調 private const string vol = "5";//範圍0~9 預設 5 音量 private string cuid = Computer.GetMacAddress();//機器Mak地址 private const string rest = "tex={0}&lan={1}&per={2}&ctp={3}&cuid={4}&tok={5}&spd={6}&pit={7}&vol={8}"; /// <summary> /// tex 即:被轉化的文本 可以是中英文結合體,如:hello 陳臥龍 /// </summary> /// <param name="tex"></param> /// <returns></returns> public string GetBaiduUri(string tex) { string Uri = "http://tsn.baidu.com/text2audio?" + string.Format(rest, tex, lan, per, ctp, cuid, tok, spd, pit, vol); return Uri; } /// <summary> /// 擷取百度Token 百度Token有效請為30天 /// </summary> /// <returns></returns> public static string GetBaiduToken() { //string Uri = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=dtjDLFlWaT1lz3CAifBNhAm7&client_secret=97dd50144c85d92cc215d6ff06202666"; //string page = GetPage(Uri, ""); //string Token = GetJsonValue(page, "access_token"); return "24.926b57f9665a644fa7b38c76c021834d.2592000.1496304195.282335-9593280"; } #region 擷取網路資源 /// <summary> /// 擷取網路資源 /// </summary> /// <param name="posturl">請求的URL</param> /// <param name="postData">發送的資料</param> /// <returns>json格式的字串</returns> public static string GetPage(string posturl, string postData) { //WX_SendNews news = new WX_SendNews(); //posturl: news.Posturl; //postData:news.PostData; System.IO.Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 準備請求... try { // 設定參數 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //發送請求並擷取相應回應資料 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程式才開始向目標網頁發送Post請求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回結果網頁(html)代碼 string content = sr.ReadToEnd(); string err = string.Empty; return content; } catch (Exception ex) { string err = ex.Message; return string.Empty; } } #endregion #region 擷取Json字串某節點的值 /// <summary> /// 擷取Json字串某節點的值 /// </summary> public static string GetJsonValue(string jsonStr, string key) { string result = string.Empty; if (!string.IsNullOrEmpty(jsonStr)) { key = "\"" + key.Trim(‘"‘) + "\""; int index = jsonStr.IndexOf(key) + key.Length + 1; if (index > key.Length + 1) { //先截逗號,若是最後一個,截“}”號,取最小值 int end = jsonStr.IndexOf(‘,‘, index); if (end == -1) { end = jsonStr.IndexOf(‘}‘, index); } result = jsonStr.Substring(index, end - index); result = result.Trim(new char[] { ‘"‘, ‘ ‘, ‘\‘‘ }); //過濾引號或空格 } } return result; } #endregion }
詳情請參考百度TTS API:網址:http://yuyin.baidu.com/docs/tts/133
C# 百度TTS,文本轉語音,RestAPI之Get請求