This article introduces jQuery + JSON + jPlayer to implement music query in the QQ space. If you are interested in the specific implementation process, I hope it will be helpful to you.
Demo address: http://bejson.com/demos/qqmusic/
Code download: http://www.jqdemo.com/932.html
Querying QQ music is an interface that came out a long time ago.
Here, jQuery and jPlayer are used to query QQ space music.
First of all, I would like to thank bejson for its various useful interfaces, including the QQ space music interfaces.
Its URL is: http://www.bejson.com/webInterface.php
The interface we want to use is located in the music interface bar 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 No. & p = 0.519638272547262 & g_tk = 1284234856
The core code is provided here:
1. How to obtain the gtk Parameter
The Code is as follows:
Function 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 the QQ space Interface
The Code is as follows:
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 Assembly JSON
Based on the returned JSON Interface
Let's parse the music JSON
The Code is as follows:
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 the jPlay PLAYER:
The Code is as follows:
Var playList;
Function newPlayer (data ){
PlayList = new jPlayerPlaylist ({
JPlayer: "# jquery_jplayer_1 ",
CssSelectorAncestor: "# jp_container_1"
},
Data ,{
SwfPath: "js ",
Supplied: "mp3 ",
Wmode: "window"
});
}