码迷,mamicode.com
首页 > Windows程序 > 详细

C# 百度TTS,文本转语音,RestAPI之Get请求

时间:2017-05-09 17:47:23      阅读:533      评论:0      收藏:0      [点我收藏+]

标签: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请求

标签:ext   doc   har   参考   secret   empty   service   ddr   ...   

原文地址:http://www.cnblogs.com/chenwolong/p/baiduTTS.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!