美文网首页
无标题文章

无标题文章

作者: 萧非子 | 来源:发表于2017-11-08 07:07 被阅读2次

    public class StringToAudio {
    string token = "5561738";
    string apiKey = "xUdNQCG3rHosEtTH7Ffiw4fI";
    string secretKey = "EtHymbgx054QDsAAtXMIrsT1Us1PlbSB";
    string cuid = "256641545456156185456489484";//生成guid
    string getTokenURL = "";
    string serverURL = "http://tsn.baidu.com/text2audio";
    public StringToAudio()
    { cuid = Guid.NewGuid().ToString();
    getToken();
    }

    region getToken

    private void getToken()
    {
    getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" + "&client_id=" + apiKey + "&client_secret=" + secretKey; token = GetValue("access_token");
    }
    private string GetValue(string key)
    {
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(getTokenURL);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader1 = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    string ssss = reader1.ReadToEnd().Replace(""", "").Replace("{", "").Replace("}", "").Replace("\n", "");
    string[] indexs = ssss.Split(',');
    foreach (string index in indexs)
    { string[] _indexs = index.Split(':');
    if (_indexs[0] == key) return _indexs[1];
    }
    return "";
    }

    endregion

    public string stringToAudio(string data, string fileName)
    { serverURL += "?tex=" + data + "&lan=zh&cuid=" + cuid + "&ctp=1&tok=" + token;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(serverURL));
    request.Timeout = 30000;
    request.Method = "GET";
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
    using (Stream stream = response.GetResponseStream())
    { byte[] buffer = new byte[1024];
    FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
    int count = -1;
    while (count != 0)
    {
    count = stream.Read(buffer,0,buffer.Length); fs.Write(buffer, 0, count);
    }
    fs.Flush();
    fs.Close();
    }
    }
    return fileName;
    }

    }

    相关文章

      网友评论

          本文标题:无标题文章

          本文链接:https://www.haomeiwen.com/subject/ptxemxtx.html