一直都想做网络音乐播放器,但是自己又没有服务器,根本就不能实现,也没那个能力实现。唯一的办法就是借助别人的API。
网上公布的API特别少,像能够直接得到音乐文件的真是地址的几乎没有,有的也只是截取流量看到的。希望做音乐的大公司能够通过开放平台共享一些API。
方法一.http://box.zhangmen.baidu.com/x?op=12&count=1&title=song$$
这个地址是最常用的,将song改为想要的歌名就可以得到歌曲的地址信息集,返回的是XML文件,得到的地址要应该解析才能用
<url> <encode> <![CDATA[ http://zhangmenshiting.baidu.com/data2/music/29969688/Y2tsam5sb3Bfn6NndK6ap5WXcJeXm2hok2tlmWqcbGxlk5eabppncJdjlmeXm5ueZpVpZGVpbGmWlWmYbJppbWpma1qin5t1YWBmbW1ra2tkaWdlamlnbTE$ ]]> </encode> <decode> <![CDATA[ 29969688.mp3?xcode=cbe10b92e5f544adf9d08f1c3bedf5c600351ec6d7d25948&mid=0.39854337415305 ]]> </decode> <type>8</type> <lrcid>0</lrcid> <flag>0</flag> </url>
解析方法:XmlDocument类,下面是关键代码,URL为真实地址
XmlDocument xml = new XmlDocument(); xml.LoadXml(info);//info就是xml内容 foreach (XmlElement item in xml.DocumentElement.ChildNodes) { if (item.InnerText == "0") return null; string deurl, enurl, lrc; if (item.Name == "url") { deurl = item.SelectSingleNode("encode").InnerText; enurl = item.SelectSingleNode("decode").InnerText; lrc = item.SelectSingleNode("lrcid").InnerText; if (lrc == "0") { lrc = "暂无歌词"; } else { lrc = strLrcPath + (Int32.Parse(lrc) / 100).ToString() + "/" + lrc + ".lrc"; } int id = deurl.LastIndexOf(‘/‘); string URL=deurl.Substring(0, id + 1) + enurl; } }
方法二.http://sug.music.baidu.com/info/suggestion?format=json&version=2&from=0&word=song&_=1405404358299
将song改为歌名,可得到歌曲的基本信息集(歌曲的id,名称,演唱)
{"data":{"song":[{"songid":"293547","songname":"\u4e0d\u5f97\u4e0d\u7231","artistname":"\u6f58\u73ae\u67cf,\u5f26\u5b50"},{"songid":"7315028","songname":"\u4e0d\u5f97\u4e0d\u7231","artistname":"\u5f26\u5b50,\u6f58\u73ae\u67cf"},{"songid":"2119510","songname":"\u4e0d\u5f97\u4e0d\u7231","artistname":"\u5f26\u5b50"},{"songid":"7332815","songname":"\u4e0d\u5f97\u4e0d\u7231","artistname":"\u60c5\u6b4c\u4f34\u4fa3"},{"songid":"274710","songname":"\u4e0d\u5f97\u4e0d\u7231","artistname":"\u5f6d\u4f73\u6167"},{"songid":"1204608","songname":"\u4e0d\u5f97\u4e0d\u7231","artistname":"\u502a\u96c5\u4e30"},{"songid":"59942403","songname":"\u4e0d\u5f97\u4e0d\u7231","artistname":"\u7fa4\u661f"},{"songid":"84940143","songname":"\u4e0d\u5f97\u4e0d\u7231","artistname":"\u8bb8\u5d69,Britney Spears"},{"songid":"1288459","songname":"\u4e0d\u5f97\u4e0d\u7231_\u6f58\u73ae\u67cf","artistname":"\u821e\u66f2\u5927\u9605\u5175"},{"songid":"64773595","songname":"\u4e0d\u5f97\u4e0d\u7231dj - \u6f58\u73ae\u67cfvs\u5f26\u5b50 mcyy mix","artistname":"MCyy"}],"artist":[],"album":[{"albumid":"83672939","albumname":"\u4e0d\u5f97\u4e0d\u7231","artistname":"\u6587\u6587\u4e50\u9053","artistpic":"http:\/\/a.hiphotos.baidu.com\/ting\/pic\/item\/f3d3572c11dfa9ec81637f0c60d0f703918fc15f.jpg"}]},"Pro":["song","album"]}
解析方法:利用Newtonsoft.Json
得到了歌曲的id之后,使用http://ting.baidu.com/data/music/links?songIds=mid&format=json,将mid改为歌曲的id就可以查询详细信息,songLink就是歌曲的真实地址
{"errorCode":22000, "data":{"xcode":"c91c710aab6c9acd577895e58b3b93ef", "songList": [{"queryId":"293547","songId":293547,"songName":"\u4e0d\u5f97\u4e0d\u7231", "artistId":"6113,1427","artistName":"\u6f58\u73ae\u67cf,\u5f26\u5b50", "albumId":2452656,"albumName":"Will\u0027S \u672a\u6765\u5f0f", "songPicSmall":"http:\/\/c.hiphotos.baidu.com\/ting\/pic\/item\/http:\/\/qukufile2.qianqian.com\/data2\/pic\/115458867\/115458867.jpg.jpg", "songPicBig":"http:\/\/c.hiphotos.baidu.com\/ting\/pic\/item\/http:\/\/qukufile2.qianqian.com\/data2\/pic\/115458852\/115458852.jpg.jpg", "songPicRadio":"http:\/\/c.hiphotos.baidu.com\/ting\/pic\/item\/http:\/\/qukufile2.qianqian.com\/data2\/pic\/115458827\/115458827.jpg.jpg", "lrcLink":"\/data2\/lrc\/13802189\/13802189.lrc","version":"","copyType":1,"time":279,"linkCode":22000, "songLink":"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/42825245\/2935471405447261.mp3?xcode=c91c710aab6c9acd795318c09ac1e37e069471290618b128", "showLink":"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/42825245\/2935471405447261.mp3?xcode=c91c710aab6c9acd795318c09ac1e37e069471290618b128", "format":"mp3","rate":128,"size":4471745,"relateStatus":"0","resourceType":"0"}]}}
方法三(测试无效):
http://mp3.baidu.com/dev/api/?tn=getinfo&ct=0&ie=utf-8&word=song&format=json
这个地址之前可以用,但后来返回的内容老是为空
http://play.baidu.com/data/music/songinfo
这个地址需要以POST方式提交,Data为songIds=mid (mid是歌曲id)
http://play.baidu.com/data/music/songlink
这个地址需要以POST方式提交,Data为songIds=mid&hq=1&type=m4a%2Cmp3&rate=&pt=0&flag=-1&s2p=-1&prerate=-1&bwt=-1&dur=-1&bat=-1&bp=-1&pos=-1&auto=-1 (mid是歌曲id)
关于使用音乐api,请参考本文,由于这些api没有发布,所以可能某天就失效了,我会随时更新这个api
请勿转载
原文地址:http://www.cnblogs.com/liuxiaobo93/p/3851798.html