winform音频播放器(有声小说[凡人修仙传])

winform音频播放器(有声小说[凡人修仙传])

该程序采用多线程的技术及DataGridView单元格扩展的技术

1.获取下载列表

        private void GetDownList()
{
//System.Web.HttpUtility.UrlDecode(解密
try
{
List<Model.Novel> list = new List<Model.Novel>();
WebResponse response = null;
StreamReader reader = null;
string Result = "";
Uri uri = new Uri("请求的有声小说地址"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.AbsoluteUri);
request.Method = "GET";
response = request.GetResponse();
reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
Result = reader.ReadToEnd();
var htmlDocument = new HtmlAgilityPack.HtmlDocument();
htmlDocument.LoadHtml(Result);
var html = htmlDocument.DocumentNode;
var title = html.CssSelect("title");
foreach (var item in title)
{
//MessageBox.Show(item.InnerHtml.ToString());
}
int i = 0;
int m = 1;
var ul = html.CssSelect("ul");
foreach (var item in ul)
{
if (i == 8)
{
foreach (var item1 in item.CssSelect("li"))
{
string no = m < 10 ? "00" + m : m < 100 ? "0" + m : m.ToString();
Model.Novel novel = new Model.Novel();
novel.Title = "凡人修仙传" + no;//item1.CssSelect("a").ToArray()[0].InnerText;
novel.DownDesc = "我要下载";
novel.PlayFile = "播放";
novel.DownPath = "下载地址/凡人修仙传/凡人修仙传" + no + ".mp3";//item1.CssSelect("a").ToArray()[0].Attributes["href"].Value;
list.Add(novel);
m++;
}
}
i++;
}
dgvFileList.DataSource = list;
}
catch (Exception ex)
{
WriteError(ex.Message);
Application.Exit();
} }

  

2.下载文件的代码

public void DownloadFile(object RowIndex)
{
int index = RowIndex.GetDBNULLValue(0);
string url = dgvFileList.Rows[index].Cells["DownPath"].Value.ToString();
string filename = lblPath.Text + "\\" + dgvFileList.Rows[index].Cells["Title"].Value.ToString() + ".mp3";
float percent = 0;
try
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
long totalBytes = myrp.ContentLength;
System.IO.Stream st = myrp.GetResponseStream();
System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
long totalDownloadedByte = 0;
byte[] by = new byte[1024];
DateTime StartTime = DateTime.Now;
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
System.Windows.Forms.Application.DoEvents();
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length); DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime.Subtract(StartTime);
double iSpeed = totalDownloadedByte / ts.TotalSeconds;
dgvFileList[3, index].Value = Model.FileOperate.GetAutoSizeString(iSpeed, 2) + "/s"; percent = (float)totalDownloadedByte / (float)totalBytes * 100;
dgvFileList[2, index].Value = percent.GetDBNULLValue(0);
}
dgvFileList[3, index].Value = "下载完成,耗时" + Math.Round(DateTime.Now.Subtract(StartTime).TotalSeconds, 2) + "s";
so.Close();
st.Close();
}
catch (System.Exception ex)
{
WriteError(ex.Message);
}
}
上一篇:react初探(二)之父子组件通信、封装公共组件


下一篇:PL/SQL编程(1) - 存储过程,函数以及参数