C # Baidu TTS, text to speech, Restapi GET request

Source: Internet
Author: User

Because of the use, so make a record:

The code is as follows:

 Public classBaiduttsservice:ibaiduttsservice { Public stringTok =Getbaidutoken (); Private Const stringLAN ="ZH";//language        Private Const stringper ="4";//pronunciation People choose 0 Women 1 Men default female 0 for female, 1 for male, 3 for emotional synthesis-degrees carefree, 4 for emotional synthesis-degrees ya ya, default to ordinary female voice        Private Const stringCTP ="1";//client type selection Web side is 1        Private Const stringSPD ="3";//Range 0~9 default 5 speed        Private Const stringPit ="4";//Range 0~9 Default 5 tones        Private Const stringvol ="5";//Range 0~9 Default 5 volume        Private stringcuid = computer.getmacaddress ();//Machine Mak Address        Private Const stringRest ="Tex={0}&lan={1}&per={2}&ctp={3}&cuid={4}&tok={5}&spd={6}&pit={7}&vol={8} "; /// <summary>        ///Tex is: the translated text can be a combination of Chinese and English, such as: Hello Chen Wolong/// </summary>        /// <param name= "Tex" ></param>        /// <returns></returns>         Public stringGetbaiduuri (stringTex) {            stringUri ="Http://tsn.baidu.com/text2audio?"+string.            Format (rest, Tex, LAN, per, CTP, cuid, Tok, SPD, Pit, vol); returnUri; }        /// <summary>        ///get Baidu token Baidu token valid please for 30 days/// </summary>        /// <returns></returns>         Public Static stringGetbaidutoken () {//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"; }        #regionGet network resources/// <summary>        ///Get network resources/// </summary>        /// <param name= "PostURL" >the requested URL</param>        /// <param name= "PostData" >the data sent</param>        /// <returns>JSON-formatted string</returns>         Public Static stringGetPage (stringPostURL,stringpostdata) {            //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); //Prepare request ...            Try            {                //Setting ParametersRequest = WebRequest.Create (PostURL) asHttpWebRequest; Cookiecontainer Cookiecontainer=NewCookiecontainer (); 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 (); //send request and get corresponding response dataResponse = Request. GetResponse () asHttpWebResponse; //until request. The GetResponse () program only starts sending a POST request to the destination Web pageInstream =Response.                GetResponseStream (); SR=NewStreamReader (instream, encoding); //return result Web page (HTML) code                stringContent =Sr.                ReadToEnd (); stringErr =string.                Empty; returncontent; }            Catch(Exception ex) {stringErr =Ex.                Message; return string.            Empty; }        }        #endregion        #regionGets the value of a node in a JSON string/// <summary>        ///gets the value of a node in a JSON string/// </summary>         Public Static stringGetjsonvalue (stringJSONSTR,stringkey) {            stringresult =string.            Empty; if(!string. IsNullOrEmpty (JSONSTR)) {Key="\""+ key. Trim ('"') +"\""; intindex = Jsonstr.indexof (key) + key. Length +1; if(Index > key. Length +1)                {                    //cut the comma first, if the last, intercept "}" number, take the minimum value                    intEnd = Jsonstr.indexof (',', index); if(End = =-1) {End= Jsonstr.indexof ('}', index); } result= jsonstr.substring (index, End-index); Result= result. Trim (New Char[] {'"',' ','\ ''});//filter Quotes or spaces                }            }            returnresult; }        #endregion           }

For more information, please refer to Baidu TTS API: URL: http://yuyin.baidu.com/docs/tts/133

C # Baidu TTS, text to speech, Restapi GET request

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.