Java: 根据网易云音乐URL下载歌曲、歌词、专辑封面和MV

先看最终效果图:

Java: 根据网易云音乐URL下载歌曲、歌词、专辑封面和MV_第1张图片

前提请下载:

(1)jsoup-1.11.3.jar

(2)JMF 2.1.1e

工作原理如下:输入一个网易云音乐URL,如:http://music.163.com/#/song?id=5048563

然后使用开源的JSoup分析框架源代码得到如下:

以及:

这样,就获得了该音乐的基础信息和MV地址(如果有MV的话)

比较重要的网址如下(外链MP3):

http://music.163.com/song/media/outer/url?id=5048563

自动解析后,返回下载地址:http://m10.music.126.net/20180717183319/797111ebf9f62bbb19e78e94765fb58b/ymusic/0707/bc0b/6847/809ceab63a4d5fbd953ca7ea9a240519.mp3

用Java下载文件即可。同理下载专辑封面和MV也是如此分析。

获取歌词网址如下:

http://music.163.com/api/song/media?id=5048563

这样就获得了歌词。如果有LRC歌词,则默认显示LRC,否则是普通歌词,否则没有歌词(暂无或纯音乐)。

实例歌词如下:

{"songStatus":3,"lyricVersion":9,"lyric":"[00:11.780]So no one told you life was gonna be this way:\n[00:15.920]your jobs a joke, you're broke, your love life's D.O.A.\n[00:20.880]It's like you're always stuck in second gear,\n[00:24.980]And it hasn't been your day, your week, your month, or even your year, but\n[00:30.660]I'll be there for you, when the rain starts to pour\n[00:35.590]I'll be there for you, like I've been there before\n[00:40.690]I'll be there for you, 'cause you're there for me too.\n[00:48.260]You're still in bed at ten and work began at eight\n[00:53.220]You've burned your breakfast, so far things are goin' great.\n[00:59.120]Your mother warned you there'd be days like these\n[01:03.450]Oh but she didn't tell you when the world has brought you down to your knees that\n[01:09.160]I'll be there for you, when the rain starts to pour\n[01:14.200]I'll be there for you, like I've been there before\n[01:18.300]I'll be there for you, 'cause you're there for me too.\n[01:28.350]No one could ever know me, no one could ever see me\n[01:35.190]Seems you're the only one who knows what it's like to be me.\n[01:40.250]Someone to face the day with, make it through all the rest with\n[01:46.960]Someone I'll always laugh with, even at my worst I'm best with you, yeah.\n[02:12.100]It's like you're always stuck in second year\n[02:16.300]And it hasn't been your day, your week, your month, or even your year.\n[02:23.340]I'll be there for you, when the rain starts to pour\n[02:28.340]I'll be there for you, like I've been there before\n[02:32.260]I'll be there for you, 'cause you're there for me too.\n[02:41.010]I'll be there for you, I'll be there for you,\n[02:50.870]I'll be there for you 'cause you're there for me too...\n[02:57.580]How you doin'?\n[02:58.580]\n","code":200}

需要注意的是,歌词里可能会显示“\n”或者"\r",此时就需要String.format()转换格式或将"\\r\\n"转换成"\r\n",以便保存到文件里。(庆祝微软记事本重大升级)

适当修改格式即可正常显示。效果如下:

Java: 根据网易云音乐URL下载歌曲、歌词、专辑封面和MV_第2张图片

选择适当选项即可下载。下载后:

Java: 根据网易云音乐URL下载歌曲、歌词、专辑封面和MV_第3张图片

Java: 根据网易云音乐URL下载歌曲、歌词、专辑封面和MV_第4张图片

剩下附加的诸多功能就不说了。关键代码如下:

HTTPAnalyzer.java

// Coding starts here
// Version 3.0 专用

import java.io.IOException;

import org.jsoup.Jsoup;  // jsoup-1.11.3

/**
 * HTTPAnalyzer类。分析网页框架,获得有用信息
 * @author 赵利昂
 * @since 0.5
 */
public class HTTPAnalyzer
{
    private String myURL = "";  // 用户输入的URL地址
    private String URL_id = "";  // 解析得到的ID
    private String frameURL = "";  // 网页框架地址
    private String bodyCode = "";  // 框架源代码
    private String essentialPart = "";  // 用于分析数据的部分
    private String info[];  // 歌曲信息
    private String lrc;  // LRC歌词(带时间标签,可能无歌词,需要去MainFrame判断)
    private String lrcUrl;  // LRC歌词地址
    private boolean hasMV = false;
    private String mvUrl = "NULL";  // MV地址(若无则返回“NULL”)
    
    /**
     * 获取URL框架中有用的信息
     * @param myURL 要分析的URL
     * @throws IOException
     */
    public HTTPAnalyzer(String myURL) throws IOException
    {
        MainFrame.logEvent("尝试解析网页框架地址");
        if(myURL.toLowerCase().startsWith("music.163"))
            this.myURL = "http://" + myURL;
        else
            this.myURL = myURL;
        
        this.URL_id = myURL.substring(myURL.lastIndexOf("song?id=") + 8);
        this.frameURL = "http://music.163.com/song?id=" + this.URL_id;
        this.bodyCode = Jsoup.connect(this.frameURL).timeout(3000).execute().body();
        this.essentialPart = bodyCode.substring(
                bodyCode.indexOf("
                    
                    

你可能感兴趣的:(Java: 根据网易云音乐URL下载歌曲、歌词、专辑封面和MV)