C#在线播放音乐
2014-01-24 16:32
239 查看
using System.Xml; using System.IO; using System.Net; public string GetMP3URL(string fString) { try { string strAPI = "http://box.zhangmen.baidu.com/x?op=12&count=1&title="; //http://box.zhangmen.baidu.com/x?op=12&count=1&title=My%20love$$ss$$$$ string[] Fstrings=fString.Split('-'); if (Fstrings.Length == 2) { strAPI = strAPI + Fstrings[0] + "$$" + Fstrings [1]+ "$$$$"; } else { strAPI = strAPI + Fstrings[0] + "$$"; } XmlTextReader hfXMLReader = new XmlTextReader(strAPI); DataSet ds = new DataSet(); ds.ReadXml(hfXMLReader); string strDecode = ds.Tables["url"].Rows[0]["decode"].ToString().Replace("\n", ""); //读取歌曲名称 string strEncode = ds.Tables["URL"].Rows[0]["encode"].ToString().Replace("\n", ""); //读取歌曲编码 //string strLrc = ds.Tables["URL"].Rows[0]["type"].ToString().Replace("<br />", ""); //读取歌词ID string strLrc = ds.Tables["URL"].Rows[0]["lrcid"].ToString().Replace("<br />", ""); //读取歌词ID string strPath = ""; string strExt = ""; string[] strPre = strEncode.Split("/".ToCharArray()); strPath = strEncode.Replace(strPre[strPre.Length - 1], strDecode); //赋值MP3真正地址 string strLrcPath = "http://box.zhangmen.baidu.com/bdlrc/"; //歌词基本地址 if (strLrc == "0") { strLrc = "暂无歌词"; } else { strLrc = strLrcPath + (Int32.Parse(strLrc) / 100).ToString() + "/" + strLrc + ".lrc"; } switch (ds.Tables["url"].Rows[0]["type"].ToString()) { case "1": strExt = "rm"; break; case "0": strExt = "mp3"; break; case "2": strExt = "wma"; break; } if (strEncode == "nothing") { return "nothing"; } return strEncode + " " + strDecode + " " + strExt + " " + strLrc + " " + strPath; } catch { return "nothing"; } } private void kryptonButton2_Click(object sender, EventArgs e) { try { string strEncode = ""; string strDecode = ""; string strLrc = ""; string strExt = ""; string strPath = ""; string[] AppString; string AppAPI = GetMP3URL(kryptonTextBox1.Text); if (AppAPI == "nothing") { MessageBox.Show("找不到音乐 " + kryptonTextBox1.Text + " 请更换查询名称"); return; } else { AppString = AppAPI.Split(" ".ToCharArray()); strEncode = AppString[0].ToString(); //编码 strDecode = AppString[1].ToString(); //解码 strExt = AppString[2].ToString(); //扩展名 strLrc = AppString[3].ToString(); //歌词URL strPath = AppString[4].ToString(); //歌曲URL this.Controls.Add(wmp); wmp.URL = strPath; kryptonLabel8.Text = kryptonTextBox1.Text; label1.Text = getWebContent(strLrc); int ImageInde = ReturnImage(); this.pictureBox10.ImageLocation = Application.StartupPath + "\\" + ImageInde + ".jpg"; } } catch { MessageBox.Show("找不到音乐 " + kryptonTextBox1.Text + " 请更换查询名称"); } } private string getWebContent(string url) { try { StringBuilder sb = new StringBuilder(""); WebRequest request = WebRequest.Create(url); request.Timeout = 10000;//10秒请求超时 StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.GetEncoding("GB2312")); while (sr.Peek() >= 0) { sb.Append(sr.ReadLine()); } return sb.ToString(); } catch (WebException ex) { return ex.Message; } }
相关文章推荐
- C#中Trim()、TrimStart()、TrimEnd()的用法介绍
- C#中静态与非静态方法比较
- .NET中高速缓存使用技巧
- C#,往线程里传参数的方法总结
- C#建立缓存的问题
- C#.ToString()格式大全
- C# 生成word 文档 代码 外加 IIS报错解决方案
- C#
- 在C#中使用List编程
- [CLR via C#]5.3 值类型的装箱和拆箱
- C# set和get如何用
- c#委托和事件
- C#使用Socket登陆WordPress源码
- 一些C#实用的方法汇总
- 【彩票】彩票预测算法(一):离散型马尔可夫链模型C#实现
- C#調用User32api
- (转)C#中Trim()、TrimStart()、TrimEnd()的用法 .
- C#中如何按字节数截取字符串?
- C# checkedListBox控件 鼠标点击一下选中
- C# Winform窗口只打开一次