百度MP3音乐API接口及应用

当你在百度去搜索一首歌时,你会发现有种更简单的方法,嘿嘿,百度有个不公开的API

http://box.zhangmen.baidu.com/x?op=12&count=1&title=大约在冬季$$齐秦$$$$
用上面的地址,蓝色部分改成歌名与作者名,然后百度就会给你一个XML:

<?xml version="1.0" encoding="gb2312" ?>
<result>
<count>1</count>
<data>
<encode>http://song.feifa-radio.com/Q/20050701/jingxuan/YjI$.Wma</encode>
<decode>1.Wma</decode>
<type>2</type>
<lrcid>49684</lrcid>
</data>
</result>


其中的count值为1是说返回的是一个,这个没什么用,接下来的东西就有用了,encode里的值是歌曲加密后的地址,加密只是对文件名加密的,我们需要的只是前面的路径,也就是 http://song.feifa-radio.com/Q/20050701/jingxuan/ 这部分,然后复制decode 的值: 1.Wma 与前面的相拼就是正确的下载地址:
http://song.feifa-radio.com/Q/20050701/jingxuan/1.Wma
后面的type的值为2表示此歌曲文件类型是wma的,其它的:1表示rm,0表示MP3,通常我们下的类型都是MP3或WMA的,所以只要有2或0的

 
lrcid这个的值是百度服务器上这首歌的歌词文件的文件名,这个文件的路径是:http://box.zhangmen.baidu.com/bdlrc/496/49684.lrc  

这个地址解析下:
http://box.zhangmen.baidu.com/bdlrc/ 这个是百度lrc歌词存放地址,后面的496是一个的不定的,就是说歌曲不同那个目录名也不同,它的算法是拿歌词文件名(也就是上面说的 49684) 除以一百,然后取小于等于其结果的最大整数,如上面的:49684/100 =496.84 小于等于496.84 的最大整数就是496,于是这首歌完整的歌词地址就出来了:http://box.zhangmen.baidu.com/bdlrc/496/49684.lrc

示例
1 using System;
2  using System.Collections.Generic;
3  using System.ComponentModel;
4  using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Xml;
8 using System.Windows.Forms;
9
10 namespace BaiDuAPI
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17
18 this.skinEngine1.SkinFile = "vista1_green.ssk";
19 }
20
21 private void button1_Click(object sender, EventArgs e)
22 {
23 System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
24
25
26
27 try
28 {
29 string strEncode = "";
30
31 string strDecode = "";
32
33 string strLrc = "";
34
35 string strExt = "";
36
37 string strPath = "";
38
39 string[] AppString;
40
41 string AppAPI = GetMP3URL(textBox4.Text);
42
43 if (AppAPI == "nothing")
44 {
45 MessageBox.Show("找不到音乐 " + textBox4.Text + " 请更换查询名称");
46
47 return;
48 }
49 else
50 {
51 AppString = AppAPI.Split(" ".ToCharArray());
52
53 strEncode = AppString[0].ToString(); //编码
54
55 strDecode = AppString[1].ToString(); //解码
56
57 strExt = AppString[2].ToString(); //扩展名
58
59 strLrc = AppString[3].ToString(); //歌词URL
60
61 strPath = AppString[4].ToString(); //歌曲URL
62
63 textBox1.Text = strEncode;
64
65 textBox2.Text = strDecode;
66
67 textBox3.Text = strPath;
68
69 textBox5.Text = strExt;
70
71 textBox6.Text = strLrc;
72 }
73 }
74 catch
75 {
76 MessageBox.Show("找不到音乐 " + textBox4.Text + " 请更换查询名称");
77 }
78
79 }
80
81 public string GetMP3URL(string fString)
82 {
83 try
84 {
85 string strAPI = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=";
86
87 strAPI = strAPI + fString;
88
89 XmlTextReader hfXMLReader = new XmlTextReader(strAPI);
90
91 DataSet ds = new DataSet();
92
93 ds.ReadXml(hfXMLReader);
94
95 string strDecode = ds.Tables["data"].Rows[0]["decode"].ToString().Replace("\n", ""); //读取歌曲名称
96
97 string strEncode = ds.Tables["data"].Rows[0]["encode"].ToString().Replace("\n", ""); //读取歌曲编码
98
99 string strLrc = ds.Tables["data"].Rows[0]["lrcid"].ToString().Replace("<br />", ""); //读取歌词ID
100
101 string strPath = "";
102
103 string strExt = "";
104
105 string[] strPre = strEncode.Split("/".ToCharArray());
106
107 strPath = strEncode.Replace(strPre[strPre.Length - 1], strDecode); //赋值MP3真正地址
108
109 string strLrcPath = "http://box.zhangmen.baidu.com/bdlrc/"; //歌词基本地址
110
111 if (strLrc == "0")
112 {
113 strLrc = "暂无歌词";
114 }
115 else
116 {
117 strLrc = strLrcPath + (Int32.Parse(strLrc) / 100).ToString() + "/" + strLrc + ".lrc";
118 }
119
120 switch (ds.Tables["data"].Rows[0]["type"].ToString())
121 {
122 case "1":
123 strExt = "rm";
124 break;
125 case "0":
126 strExt = "mp3";
127 break;
128 case "2":
129 strExt = "wma";
130 break;
131 }
132
133 if (strEncode == "nothing")
134 {
135 return "nothing";
136 }
137
138 return strEncode + " " + strDecode + " " + strExt + " " + strLrc + " " + strPath;
139 }
140 catch
141 {
142 return GetMP3URL(fString);
143 }
144 }
145
146 private void button2_Click(object sender, EventArgs e)
147 {
148 string tempstr = "";
149
150 for (int i = 1; i < 11; i++)
151 {
152 tempstr += "1<<" + i.ToString() + ": " + (1 << i).ToString() + "\r\n";
153 }
154
155 MessageBox.Show(tempstr);
156 }
157
158 private void button2_Click_1(object sender, EventArgs e)
159 {
160 this.axWindowsMediaPlayer1.URL = textBox3.Text;
161 }
162 }
163
164
原文地址:https://www.cnblogs.com/jiewei915/p/1875809.html