由于想写一个Android练手项目,可以听取b站视频(虽然b站自带了听视频功能,但是我就是想写,略略略)
所以需要android的MediaPlayer来播放b站视频
需要获得b站视频的原视频链接,并有权限下载它
中间走过很多弯路,最后终于在很多博客的帮助下解决了
这篇博客最有用:
最后也是成功实现了播放b站视频(的音频)
请求下面这个api
1 | https://api.bilibili.com/x/player/playurl?otype=json&fnver=0&fnval=2&player=1&qn=64&bvid=[bv号]&cid=[视频cid] |
[bv号] 和 [视频cid] 替换为目标视频的bv和cid
返回结果示例
1 | { |
1 | "url": "https://upos-sz-mirror08c.bilivideo.com/upgcxcode/01/45/1402934501/1402934501-1-192.mp4?e=ig8euxZM2rNcNbNghwdVhwdlhbNVhwdVhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1707379216&gen=playurlv2&os=08cbv&oi=0&trid=206616dd6bd04cb7bb58a09abf41cbcau&mid=327352538&platform=pc&upsig=e8187336e7f406570036048adef804c3&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=0,3&buvid=18C67622-1474-77DB-5432-360FB136820167761infoc&build=0&f=u_0_0&agrr=1&bw=222454&logo=80000000", |
这个url即为视频原链接
注:这个链接会变化
可请求下面这个api
1 | https://api.bilibili.com/x/web-interface/view?bvid=[bv号] |
返回结果示例
1 | { |
1 | "cid": 1431164754, |
即可获得cid
1 | String url = "https://upos-sz-mirror08c.bilivideo.com/upgcxcode/01/45/1402934501/1402934501-1-192.mp4?e=ig8euxZM2rNcNbNghwdVhwdlhbNVhwdVhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1707379216&gen=playurlv2&os=08cbv&oi=0&trid=206616dd6bd04cb7bb58a09abf41cbcau&mid=327352538&platform=pc&upsig=e8187336e7f406570036048adef804c3&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=0,3&buvid=18C67622-1474-77DB-5432-360FB136820167761infoc&build=0&f=u_0_0&agrr=1&bw=222454&logo=80000000"; |
这样会播放视频的音频
可通过相关代码播放视频
headers.put("Referer","https://www.bilibili.com"); headers.put("Sec-Fetch-Mode", "no-cors"); headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36");