首页 > 其他 > 百度MP3音乐API接口及应用

百度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

安宁ヤ太天真似乎说得复杂了了,呵呵,希望对大家有用

作者: 安宁ヤ太天真
网址: http://hi.baidu.com/kiss3344
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。

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;
       }
   }

原文:http://shuhejiang.blog.163.com/blog/static/117667532009111533750457/

分类: 其他 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.

=2加6(必填)请输入两数相加的结果。