using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;
namespace BaiDuAPI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.skinEngine1.SkinFile = "vista1_green.ssk";
}
private void button1_Click(object sender, EventArgs e)
{
System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
try
{
string strEncode = "";
string strDecode = "";
string strLrc = "";
string strExt = "";
string strPath = "";
string[] AppString;
string AppAPI = GetMP3URL(textBox4.Text);
if (AppAPI == "nothing")
{
MessageBox.Show("找不到音乐 " + textBox4.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
textBox1.Text = strEncode;
textBox2.Text = strDecode;
textBox3.Text = strPath;
textBox5.Text = strExt;
textBox6.Text = strLrc;
}
}
catch
{
MessageBox.Show("找不到音乐 " + textBox4.Text + " 请更换查询名称");
}
}
public string GetMP3URL(string fString)
{
try
{
string strAPI = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=";
strAPI = strAPI + fString;
XmlTextReader hfXMLReader = new XmlTextReader(strAPI);
DataSet ds = new DataSet();
ds.ReadXml(hfXMLReader);
string strDecode = ds.Tables["data"].Rows[0]["decode"].ToString().Replace("\n", ""); //读取歌曲名称
string strEncode = ds.Tables["data"].Rows[0]["encode"].ToString().Replace("\n", ""); //读取歌曲编码
string strLrc = ds.Tables["data"].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["data"].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 GetMP3URL(fString);
}
}
private void button2_Click(object sender, EventArgs e)
{
string tempstr = "";
for (int i = 1; i < 11; i++)
{
tempstr += "1<<" + i.ToString() + ": " + (1 << i).ToString() + "\r\n";
}
MessageBox.Show(tempstr);
}
private void button2_Click_1(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.URL = textBox3.Text;
}
}
}
C# 百度音乐搜索API利用
最新推荐文章于 2024-01-13 12:57:37 发布