Home >Web Front-end >JS Tutorial >jQuery+JSON+jPlayer implements QQ space music query
Querying QQ Music is an interface that came out a long time ago.
jQuery and jPlayer are used here to implement QQ space music query.
The interface we want to use is located in the music interface column on the bejson interface page.
QQ music interface address:
http://qzone-music.qq.com/fcg-bin/fcg_music_fav_getinfo.fcg?dirinfo=0&dirid=1&uin=QQ号&p=0.519638272547262&g_tk=1284234856
given here core code :
1. How to obtain gtk parameters
f
unction getGTK() { var str = "@HR3etVm80"; var hash = 5381; for (var i = 0, len = str.length; i < len; ++i) { hash += (hash << 5) + str.charAt(i).charCodeAt(); } var gtk = hash & 0x7fffffff; //document.getElementById("gtk").value = gtk; return gtk; }
2. Request QQ space interface
function getMusicId() { var qqNo = document.getElementById("qqNo").value; var url = 'http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?uin=' + qqNo + '&json=1&g_tk=' + getGTK(); $.getScript(url); }
3. Callback to assemble JSON
According to the returned JSON interface
Let’s parse the music JSON
function jsonCallback(data) { if(data.code==1){ alert(data.msg); return; } var songs = data.qqmusic.playlist.song; var dataStr = "["; for (var i = 0; i < songs.length; i++) { dataStr += "{"; dataStr += "title:'" + songs[i].xsong_name + "',"; dataStr += "mp3:'" + songs[i].xsong_url + "'"; dataStr += "}"; if (i < songs.length) { dataStr += ','; } } dataStr += ']'; eval("ds=" + dataStr); newPlayer(ds); }
Finally we call jPlay player:
var playList; function newPlayer(data) { playList = new jPlayerPlaylist({ jPlayer: "#jquery_jplayer_1", cssSelectorAncestor: "#jp_container_1" }, data, { swfPath: "js", supplied: "mp3", wmode: "window" }); }