C# 百度TTS,文本转语音,RestAPI之Get请求-阿里云开发者社区

开发者社区> 开发与运维> 正文

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

简介: 因为用得到,所以作个记录: 代码如下: public class BaiduTTSService : IBaiduTTSService { public string tok = GetBaiduToken(); private const string l...

因为用得到,所以作个记录:

代码如下:

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=你的client_id&client_secret=你的client_secret";
            //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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章