大家好,又见面了,我是你们的朋友全栈君。
大家好,前段时间重写了自己的音乐播放器,源码放在github上,源码地址和项目地址下面都有,如果喜欢记得star一下哈。
由于之前给大家分享的api虽然可以用,但是版本太旧了,很多也没有了歌词,今天博主给大家分享一个最新的qq音乐api,亲测可用,话不多说直接上代码
最新音乐
let url = 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8¬ice=0&platform=h5&needNewCode=1&tpl=3&page=detail&type=top&topid=27&_=1519963122923';
$.ajax({
url:url,
type:"get",
dataType:'jsonp',
jsonp: "jsonpCallback",
scriptCharset: 'GBK',//解决中文乱码
success: function(data){
//最新音乐数据
},
error:function (e) {
console.log('error');
}
});
这里用的是jsonp跨域,json数据获取成功后可以在数据中的songList数组下找到很多条数据,这些都是最新音乐的信息,打开其中一条,找到data并展开,找到两个重要参数字段songmid以及albummid,分别用获取歌曲源和歌词以及图片用的
推荐音乐
let url = 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8¬ice=0&platform=h5&needNewCode=1&tpl=3&page=detail&type=top&topid=36&_=1520777874472';
$.ajax({
url:url,
type:"get",
dataType:'jsonp',
jsonp: "jsonpCallback",
scriptCharset: 'GBK',//解决中文乱码
success: function(data){
//推荐音乐数据
},
error:function (e) {
console.log('error');
}
});
同样jsonp获取信息,得到数据和获取最新音乐差不多
歌曲
let url = 'https://api.bzqll.com/music/tencent/url?key=579621905&id='+songSmallMessage.songmid+'br=320''
这里的songmid就是上面所说过得啦,拿进来放到audio标签中就能直接听啦
搜索
let val = '青花瓷';
let url = 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp?g_tk=5381&uin=0&format=jsonp&inCharset=utf-8&outCharset=utf-8¬ice=0&platform=h5&needNewCode=1&w='+val+'&zhidaqu=1&catZhida=1&t=0&flag=1&ie=utf-8&sem=1&aggr=0&perpage=20&n=20&p=1&remoteplace=txt.mqq.all&_=1520833663464';
$.ajax({
url:url,
type:"get",
dataType:'jsonp',
jsonp: "callback",
jsonpCallback:'callback',
scriptCharset: 'GBK',//解决中文乱码
success: function(data){
//获取搜索数据
},
error:function (e) {
console.log('error');
Indicator.close();
}
});
歌词
获取歌词也算是个坎,因为QQ因为设置了防盗链我们需要伪装一下自己,单纯用js恐怕无法实现了,我们可以用后端语言去获取,博主用的是node,里有个request的依赖包,用来修改我们的header,从而得到数据
let url = "https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg?callback=MusicJsonCallback_lrc&pcachetime=1494070301711&songmid="+songId+"&g_tk=5381&jsonpCallback=MusicJsonCallback_lrc&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0";
let options = {
url: url,
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36",
"Accept": "*/*",
"Referer": "https://y.qq.com/portal/player.html",
"Accept-Language": "zh-CN,zh;q=0.8",
"Cookie": "pgv_pvid=8455821612; ts_uid=1596880404; pgv_pvi=9708980224; yq_index=0; pgv_si=s3191448576; pgv_info=ssid=s8059271672; ts_refer=ADTAGmyqq; yq_playdata=s; ts_last=y.qq.com/portal/player.html; yqq_stat=0; yq_playschange=0; player_exist=1; qqmusic_fromtag=66; yplayer_open=1",
"Host": "c.y.qq.com",
}
};
request(options,(error, response, body)=>{
if(!error && response.statusCode == 200){
console.log(body);
}else{
console.log("error");
}
});
上面获取的数据在body中,长这个样子
"MusicJsonCallback_lrc({\"retcode\":0,\"code\":0,\"subcode\":0,\"lyric\":\"W3RpOuaDs+S9oOS4gOaVtOWknF0NClthcjrlrZnlrZDmtrVdDQpbYWw65oOz5L2g5LiA5pW05aScXQ0KW2J5Ol0NCltvZmZzZXQ6MF0NClswMDowMC4yM13mg7PkvaDkuIDm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0NS4yNV3miJHnmoTlubznqJog5piv5oiR55qE6aKG6KKWDQpbMDE6NDguNDhd5LiL5Luk5ZC75L2g6aKd5aS0DQpbMDE6NTAuNDhdClswMTo1MS4yMF3niLHmg4XlsLHmmK/or6Ug55So5p2l56ew6aKCDQpbMDE6NTQuNDZd5b+D5Yqo5LiN5Lya5Y+Y5penDQpbMDE6NTYuNjNdClswMTo1Ny4yNV3miYDosJPmhIEg5LiN6L+H5LiA56eN5b2i5a65DQpbMDI6MDAuNDNd5oiR5rKJ6YaJ5Zyo5byA5aS0DQpbMDI6MDIuODddClswMjowNC4xNF3mg7PkvaAg5oOz5L2gIOaDs+S9oA0KWzAyOjA3LjkzXQpbMDI6MDguNDdd5oOz5L2g5LiA5pW05aScDQpbMDI6MTAuMTFd5oOz5L2gIOaDs+S9oCDmg7PkvaANClswMjoxMy44MV0KWzAyOjE1LjA2XeaDs+S9oOS4gOaVtOWknCDmiJHpg73msqHnnaENClswMjoxOC40NF3lgrvnnIvnnYDov5nmmJ/nqboNClswMjoyMC41MF0KWzAyOjIxLjIyXeaIkeeahOW5vOeomiDmmK/miJHnmoTpooboopYNClswMjoyNC40NV3kuIvku6TlkLvkvaDpop3lpLQNClswMjoyNi42MF0KWzAyOjI3LjI0XeeIseaDheWwseaYr+ivpSDnlKjmnaXnp7DpooINClswMjozMC40OF3lv4PliqjkuI3kvJrlj5jml6cNClswMjozMi42M10KWzAyOjMzLjIxXeaJgOiwk+aEgSDkuI3ov4fkuIDnp43lvaLlrrkNClswMjozNi4zOV3miJHmsonphonlnKjlvIDlpLQ=\",\"trans\":\"\"})"
看到这串字符串了吗,不要方,我们先把这些数据拿到前台处理,其实他们是一个json串而已被函数MusicJsonCallback_lrc包着一同返回了,我们可以创建一个MusicJsonCallback_lrc函数并且在对这个字符串进行eval一下的得到的json字符串的lyric中的数据,这个数据是个base64形式的字符我们转换一下就可以看到歌词啦
import Base64 from 'js-base64'
eval(data);//把获取的字符串放进去,通过eval再次执行一次代码
//创建一个函数MusicJsonCallback_lrc
function MusicJsonCallback_lrc(data){
let lyric = Base64.Base64.decode(data.lyric);
console.log(lyric);//歌词
}
注意一下这里base64的解析用的是js-base64的所以记得先npm install js-base64 一下,然后引入,就可以看到歌词啦
博主自己做了一个音乐播放器如果喜欢记得star一下哈
github: https://github.com/hhzzcc/CMusic
项目地址:
效果图:
这些都是博主去QQ音乐上面一个个挖的以及参考一些大佬的博客得出来的心得,如果写的不好还请大家见谅,如果有疑问底下评论区可以提出来,或者私聊,博主会尽力即使给大家回复!
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/153177.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...