-
Notifications
You must be signed in to change notification settings - Fork 0
/
aDetailjs.js
142 lines (127 loc) · 4.66 KB
/
aDetailjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const songDataUrls = [
'https://raw.githubusercontent.com/RunlingDev/ClassAlbum/meta/songs.json',
'songs.json',
'https://example.com/songs.json',
'https://backup.example.com/songs.json'
];
function loadSongsData(callback) {
const xhr = new XMLHttpRequest();
function tryNextUrl() {
if (songDataUrls.length > 0) {
const url = songDataUrls.shift();
xhr.open('GET', url, true);
xhr.send();
} else {
console.error('Failed to load song data from all provided URLs.');
}
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
try {
const songs = JSON.parse(xhr.responseText);
callback(songs);
} catch (error) {
console.error('Failed to parse song data:', error);
tryNextUrl();
}
} else {
tryNextUrl();
}
}
};
tryNextUrl();
}
// 为音频详情页面加载媒体和歌词
const urlParams = new URLSearchParams(window.location.search);
const songTitle = urlParams.get('song');
if (songTitle) {
loadSongsData(songs => {
const song = songs.find(s => s.title === decodeURIComponent(songTitle));
if (song && song.type === 'audio') {
const songTitleElement = document.getElementById('songTitle');
const albumTitleElement = document.getElementById('albumTitle');
const mediaContainer = document.getElementById('mediaContainer');
const lyricsContainer = document.getElementById('lyricsContainer');
songTitleElement.textContent = song.title;
albumTitleElement.textContent = `${song.album} - ${song.artist}`;
mediaContainer.innerHTML = `<audio id="audioPlayer" src="${song.source}" controls></audio>`;
if (song.lrc !== 'none') {
if( song.lrc.includes("music.163.com") ){
const parser = new LrcParser();
const lyrics = parser.parse(getSongLrc(song.lrc));
displayLyrics(lyrics);
}else{
fetch(song.lrc)
.then(response => response.text())
.then(data => {
const parser = new LrcParser();
const lyrics = parser.parse(data);
displayLyrics(lyrics);
});
}
}
} else {
console.error('Song data not found or invalid media type.');
}
});
}
// 解析 .lrc 文件内容
class LrcParser {
parse(data) {
const lyrics = [];
const lines = data.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (line) {
const parts = line.split(']');
const timestamp = parseTimeString(parts[0].substr(1));
const text = parts[1];
lyrics.push({ timestamp, text });
}
}
return lyrics;
}
}
function parseTimeString(timeString) {
const parts = timeString.split(':');
const minutes = parseFloat(parts[0]);
const seconds = parseFloat(parts[1]);
return minutes * 60 + seconds;
}
// 渲染歌词
function displayLyrics(lyrics) {
const audio = document.getElementById('audioPlayer');
const lyricsDiv = document.getElementById('lyricsContainer');
const pastLyricsDiv = document.getElementById('pastLyrics');
const presentLyricsDiv = document.getElementById('presentLyrics');
const comingLyricsDiv = document.getElementById('comingLyrics');
audio.addEventListener('timeupdate', () => {
const currentTime = audio.currentTime;
try {
for (let i = 0; i < lyrics.length; i++) {
if (lyrics[i].timestamp > currentTime) {
pastLyricsDiv.innerText = lyrics[i - 2].text;
presentLyricsDiv.innerText = lyrics[i - 1].text;
comingLyricsDiv.innerText = lyrics[i].text;
lyricsDiv.scrollTop = lyricsDiv.scrollHeight;
break;
}
}
} catch {
pastLyricsDiv.innerText = "";
presentLyricsDiv.innerText = lyrics[0].text;
comingLyricsDiv.innerText = lyrics[1].text;
}
});
}
async function getSongLrc(url) {
try {
const response = await fetch(url);
const data = await response.json();
return data.lyric;
} catch (error) {
console.error('获取歌词失败:', error);
return '获取歌词失败';
}
}