资源简介
利用百度API进行语音合成,生成mp3文件供程序内部调用。附源码
代码片段和文件信息
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(“结束下载文件。。。“);
}
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
评论
共有 条评论