1. 程式人生 > >音樂播放器之QQ音樂最新api,親測可用

音樂播放器之QQ音樂最新api,親測可用


大家好,前段時間重寫了自己的音樂播放器,原始碼放在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&notice=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&notice=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 = 'http://ws.stream.qqmusic.qq.com/C100'+songmid+'.m4a?fromtag=0&guid=126548448

這裡的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&notice=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+S9oOS4gOaVtOWknF0NClthcjr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V3mg7PkvaDkuIDmlbTlpJwg5oiR6YO95rKh552hDQpbMDE6NDIuNDdd5YK755yL552A6L+Z5pif56m6DQpbMDE6NDQuNjhdClswMTo0NS4yNV3miJHnmoTlubznqJog5piv5oiR55qE6aKG6KKWDQpbMDE6NDguNDhd5LiL5Luk5ZC75L2g6aKd5aS0DQpbMDE6NTAuNDhdClswMTo1MS4yMF3niLHmg4XlsLHmmK/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音樂上面一個個挖的以及參考一些大佬的部落格得出來的心得,如果寫的不好還請大家見諒,如果有疑問底下評論區可以提出來,或者私聊,博主會盡力即使給大家回覆!