Skip to content

Commit b52ee7e

Browse files
committed
fix: 播放部分歌曲失败的问题
1 parent 4622782 commit b52ee7e

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

ZMusic-Plugin/src/main/java/me/zhenxin/zmusic/music/searchSource/NeteaseCloudMusic.java

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,71 @@ public NeteaseCloudMusic() {
2020
*/
2121
public static JsonObject getMusicUrl(String musicName) {
2222
try {
23-
if (musicName.contains("-id:")) {
24-
musicName = musicName.split("-id:")[1];
25-
}
26-
String getUrl = Config.neteaseApiRoot + "search?keywords=" + URLEncoder.encode(musicName, "UTF-8") + "&limit=1&type=1";
2723
Gson gson = new Gson();
24+
int musicId;
25+
if (!musicName.contains("-id:")) {
26+
String getUrl = Config.neteaseApiRoot + "search?keywords=" + URLEncoder.encode(musicName, "UTF-8") + "&limit=1&type=1";
27+
String jsonText = NetUtils.getNetString(getUrl, null);
28+
JsonObject json = gson.fromJson(jsonText, JsonObject.class);
29+
JsonObject result = json.getAsJsonObject("result");
30+
if (result != null || result.get("songCount").getAsInt() != 0) {
31+
JsonObject jsonOut = result.getAsJsonArray("songs").get(0).getAsJsonObject();
32+
musicId = jsonOut.get("id").getAsInt();
33+
} else {
34+
return null;
35+
}
36+
} else {
37+
musicId = Integer.parseInt(musicName.substring(musicName.indexOf("-id:") + 4));
38+
}
39+
String getUrl = Config.neteaseApiRoot + "song/detail?ids=" + musicId;
2840
String jsonText = NetUtils.getNetString(getUrl, null);
2941
JsonObject json = gson.fromJson(jsonText, JsonObject.class);
30-
JsonObject result = json.getAsJsonObject("result");
31-
if (result != null || result.get("songCount").getAsInt() != 0) {
32-
JsonObject jsonOut = result.getAsJsonArray("songs").get(0).getAsJsonObject();
33-
int musicID = jsonOut.get("id").getAsInt();
34-
JsonObject getUrlJson = gson.fromJson(NetUtils.getNetString(Config.neteaseApiRoot + "song/url?id=" + musicID + "&br=320000", null), JsonObject.class);
35-
String musicUrl = null;
36-
try {
37-
musicUrl = getUrlJson.get("data").getAsJsonArray().get(0).getAsJsonObject().get("url").getAsString();
38-
} catch (Exception e) {
39-
e.printStackTrace();
40-
}
41-
42+
JsonObject result = json.getAsJsonArray("songs").get(0).getAsJsonObject();
43+
String name = result.get("name").getAsString();
44+
int inttime = result.get("dt").getAsInt();
45+
inttime = inttime / 1000;
46+
String time = String.valueOf(inttime);
47+
JsonArray singer = result.get("ar").getAsJsonArray();
48+
String singerName = "";
49+
for (JsonElement j : singer) {
50+
singerName += j.getAsJsonObject().get("name").getAsString() + "/";
51+
}
52+
singerName = singerName.substring(0, singerName.length() - 1);
53+
54+
String lyricJsonText = NetUtils.getNetString(Config.neteaseApiRoot + "lyric?id=" + musicId, null);
55+
JsonObject lyricJson = gson.fromJson(lyricJsonText, JsonObject.class);
56+
57+
String lyric = "";
58+
String lyricTr = "";
59+
try {
60+
lyric = lyricJson.get("lrc").getAsJsonObject().get("lyric").getAsString();
61+
lyric = lyric.replaceAll("\r", "");
62+
lyricTr = lyricJson.get("tlyric").getAsJsonObject().get("lyric").getAsString();
63+
lyricTr = lyricTr.replaceAll("\r", "");
64+
} catch (Exception ignored) {
65+
}
4266

43-
String lyricJsonText = NetUtils.getNetString(Config.neteaseApiRoot + "lyric?id=" + musicID, null);
44-
JsonObject lyricJson = gson.fromJson(lyricJsonText, JsonObject.class);
45-
String name = jsonOut.get("name").getAsString();
46-
int inttime = jsonOut.get("duration").getAsInt();
47-
inttime = inttime / 1000;
48-
String time = String.valueOf(inttime);
49-
JsonArray singer = jsonOut.get("artists").getAsJsonArray();
50-
String singerName = "";
51-
for (JsonElement j : singer) {
52-
singerName += j.getAsJsonObject().get("name").getAsString() + "/";
53-
}
54-
singerName = singerName.substring(0, singerName.length() - 1);
55-
String lyric = "";
56-
String lyricTr = "";
57-
try {
58-
lyric = lyricJson.get("lrc").getAsJsonObject().get("lyric").getAsString();
59-
lyric = lyric.replaceAll("\r", "");
60-
lyricTr = lyricJson.get("tlyric").getAsJsonObject().get("lyric").getAsString();
61-
lyricTr = lyricTr.replaceAll("\r", "");
62-
} catch (Exception ignored) {
63-
}
64-
StringBuilder sb = new StringBuilder();
65-
JsonObject returnJson = new JsonObject();
66-
returnJson.addProperty("id", musicID);
67-
returnJson.addProperty("url", musicUrl);
68-
returnJson.addProperty("time", time);
69-
returnJson.addProperty("name", name);
70-
returnJson.addProperty("singer", singerName);
71-
returnJson.addProperty("lyric", lyric);
72-
returnJson.addProperty("lyricTr", lyricTr);
73-
returnJson.addProperty("error", sb.toString());
74-
return returnJson;
75-
} else {
76-
return null;
67+
JsonObject getUrlJson = gson.fromJson(NetUtils.getNetString(Config.neteaseApiRoot + "song/url/v1?id=" + musicId + "&level=exhigh", null), JsonObject.class);
68+
String musicUrl = null;
69+
try {
70+
musicUrl = getUrlJson.get("data").getAsJsonArray().get(0).getAsJsonObject().get("url").getAsString();
71+
} catch (Exception e) {
72+
e.printStackTrace();
7773
}
7874

79-
} catch (Exception e) {
75+
StringBuilder sb = new StringBuilder();
76+
JsonObject returnJson = new JsonObject();
77+
returnJson.addProperty("id", musicId);
78+
returnJson.addProperty("url", musicUrl);
79+
returnJson.addProperty("time", time);
80+
returnJson.addProperty("name", name);
81+
returnJson.addProperty("singer", singerName);
82+
returnJson.addProperty("lyric", lyric);
83+
returnJson.addProperty("lyricTr", lyricTr);
84+
returnJson.addProperty("error", sb.toString());
85+
return returnJson;
86+
} catch (
87+
Exception e) {
8088
e.printStackTrace();
8189
return null;
8290
}

0 commit comments

Comments
 (0)