您的位置:首页 > 编程语言 > C#

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: