資源簡介
利用百度API進(jìn)行語音合成,生成mp3文件供程序內(nèi)部調(diào)用。附源碼
代碼片段和文件信息
using?Newtonsoft.Json;
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Threading;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?GetSound
{
????public?partial?class?formGetSound?:?Form
????{
????????public?formGetSound()
????????{
????????????InitializeComponent();
????????}
????????private?Thread?th;
????????private?void?WriteLog(string?text)
????????{
????????????txtLog.Invoke(
????????????????new?EventHandler(
????????????????????delegate?{
????????????????????????txtLog.AppendText(Environment.NewLine?+?text?+?“。。。“);
????????????????????}
????????????????????)
????????????????);
????????}
????????public?void?StopGet()
????????{
????????????WriteLog(DateTime.Now.ToString(“yyyy-MM-dd?HH:mm:ss“));
????????????//?sr.Close();
????????????th.Abort();
????????}
????????#region?百度API獲取mp3
????????private?void?btnCreateMp3_Click(object?sender?EventArgs?e)
????????{
????????????th?=?new?Thread(RunGet);
????????????th.Start();
????????}
????????private?void?RunGet()
????????{
????????????try
????????????{
????????????????WriteLog(DateTime.Now.ToString(“yyyy-MM-dd?HH:mm:ss“));
????????????????string?url?=?string.Format(“https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={0}&client_secret={1}“?System.Configuration.ConfigurationManager.AppSettings[“APIKey“]?System.Configuration.ConfigurationManager.AppSettings[“SecretKey“]);
????????????????WriteLog(“獲取token地址:“?+?url);
????????????????string?back?=?Helper.GetUrltoHtml(url?“utf-8“);
????????????????WriteLog(“獲取token:“?+?back);
????????????????Token?t?=?JsonConvert.Deserializeobject(back);
????????????????WriteLog(“成功獲取token:“?+?t.access_token);
????????????????string?msg?=?Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(txtText.Text));
????????????????url?=?string.Format(“http://tsn.baidu.com/text2audio?tex={0}&lan=zh&cuid={1}&ctp=1&tok={2}“?msg?System.Configuration.ConfigurationManager.AppSettings[“MAC“]?t.access_token);
????????????????WriteLog(“開始下載文件。。。“);
????????????????string?Name?=?button1.Text?+?“\\“?+?txtText.Text?+?DateTime.Now.ToString(“mmss“)?+?“.mp3“;
????????????????Helper.DownFile(url?Name);
????????????????WriteLog(“結(jié)束下載文件。。。“);
????????????}
????????????catch?(Exception?e)
????????????{
????????????????WriteLog(e.Message);
????????????}
????????????finally
????????????{
????????????????StopGet();
????????????}
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????string?path?=?string.Empty;
????????????FolderBrowserDialog?fbd?=?new?FolderBrowserDialog();
????????????if?(fbd.ShowDialog()?==?DialogResult.OK)
????????????{
????????????????path?=?fbd.SelectedPath;
????????????}
????????????button1.Text?=?path;
????????????if?(!string.IsNul
評論
共有 條評論