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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
| <% function getMode(page, showWhen = [], hideWhen = []) { const hideItem = hideWhen.find((path) => { const regExp = path.startsWith('/') ? new RegExp(`^${page.lang}${path}`) : new RegExp(`^${page.lang}/${path}`) return regExp.test(page.path); }); if (hideItem) { return; } const showItem = showWhen.find(({ mode, type, path }) => { if (type === '') { return !page.type; } if (type) { return type === page.type } if (path) { return path === '/' ? new RegExp(`^${page.lang}(\/|\/index(\.html)?)?$`).test(page.path) : page.path.startsWith(`${page.lang}/${path}`) } }) return showItem?.mode; } %> <% let mode = getMode(page, theme.aplayer?.showWhen, theme.aplayer?.hideWhen) %> <% if (mode) { %> <!-- link js css of APlayer --> <% if (theme.cdns && theme.cdns.APlayer && theme.cdns.APlayer.enable) { %> <link href="<%- theme.cdns.APlayer.url.css %>" rel="stylesheet"> <script defer type="text/javascript" src="<%- theme.cdns.APlayer.url.js %>"></script> <% } else { %> <%- css(['/plugins/APlayer.min.css'])%> <%- js({ src: '/plugins/APlayer.min.js', defer: true, type: 'text/javascript' }) %> <% } %>
<div id="aplayer"></div>
<script> const mode = <%- JSON.stringify(mode || 'fixed') %>; const themeColor = <%- JSON.stringify(theme.themeColor || '#ed7d32') %>;
const originAudioList = [ { "name": "May It Be", "artist": "Enya", "url": "/assets/audio/May It Be-Enya.mp3", "lrc": "/assets/audio/May It Be-Enya.lrc", "cover": "/assets/audio/May It Be-Enya.jpg" }, { "name": "Jersey Girl", "artist": "Bruce Springsteen", "url": "/assets/audio/Jersey Girl-Bruce Springsteen.mp3", "lrc": "/assets/audio/Jersey Girl-Bruce Springsteen.lrc", "cover": "/assets/audio/Jersey Girl-Bruce Springsteen.jpg" }, { "name": "Misty Mountains", "artist": "Geoff Castellucci", "url": "/assets/audio/Misty Mountains-Geoff Castellucci.mp3", "lrc": "/assets/audio/Misty Mountains-Geoff Castellucci.lrc", "cover": "/assets/audio/Misty Mountains-Geoff Castellucci.jpg" }, { "name": "Someone There For Me", "artist": "Rebecca Blaylock", "url": "/assets/audio/Someone There For Me-Rebecca Blaylock.mp3", "lrc": "/assets/audio/Someone There For Me-Rebecca Blaylock.lrc", "cover": "/assets/audio/Someone There For Me-Rebecca Blaylock.jpg" }, { "name": "Voices", "artist": "Cheap Trick", "url": "/assets/audio/Voices-Cheap Trick.mp3", "lrc": "/assets/audio/Voices-Cheap Trick.lrc", "cover": "/assets/audio/Voices-Cheap Trick.jpg" }, { "name": "We Will Rock You", "artist": "Queen", "url": "/assets/audio/We Will Rock You-Queen.mp3", "lrc": "/assets/audio/We Will Rock You-Queen.lrc", "cover": "/assets/audio/We Will Rock You-Queen.jpg" }, { "name": "他不是你", "artist": "连诗雅", "url": "/assets/audio/他不是你-连诗雅.mp3", "lrc": "/assets/audio/他不是你-连诗雅.lrc", "cover": "/assets/audio/他不是你-连诗雅.jpg" }, { "name": "千千阕歌", "artist": "陈慧娴", "url": "/assets/audio/千千阕歌-陈慧娴.mp3", "lrc": "/assets/audio/千千阕歌-陈慧娴.lrc", "cover": "/assets/audio/千千阕歌-陈慧娴.jpg" }, { "name": "可惜我是水瓶座", "artist": "杨千嬅", "url": "/assets/audio/可惜我是水瓶座-杨千嬅.mp3", "lrc": "/assets/audio/可惜我是水瓶座-杨千嬅.lrc", "cover": "/assets/audio/可惜我是水瓶座-杨千嬅.jpg" }, { "name": "喜欢你", "artist": "Beyond", "url": "/assets/audio/喜欢你-Beyond.mp3", "lrc": "/assets/audio/喜欢你-Beyond.lrc", "cover": "/assets/audio/喜欢你-Beyond.jpg" }, { "name": "富士山下", "artist": "陈奕迅", "url": "/assets/audio/富士山下-陈奕迅.mp3", "lrc": "/assets/audio/富士山下-陈奕迅.lrc", "cover": "/assets/audio/富士山下-陈奕迅.jpg" }, { "name": "小城大事", "artist": "杨千嬅", "url": "/assets/audio/小城大事-杨千嬅.mp3", "lrc": "/assets/audio/小城大事-杨千嬅.lrc", "cover": "/assets/audio/小城大事-杨千嬅.jpg" }, { "name": "少女的祈祷", "artist": "杨千嬅", "url": "/assets/audio/少女的祈祷-杨千嬅.mp3", "lrc": "/assets/audio/少女的祈祷-杨千嬅.lrc", "cover": "/assets/audio/少女的祈祷-杨千嬅.jpg" }, { "name": "晚风心里吹", "artist": "李克勤", "url": "/assets/audio/晚风心里吹-李克勤.mp3", "lrc": "/assets/audio/晚风心里吹-李克勤.lrc", "cover": "/assets/audio/晚风心里吹-李克勤.jpg" }, { "name": "月半小夜曲", "artist": "李克勤", "url": "/assets/audio/月半小夜曲-李克勤.mp3", "lrc": "/assets/audio/月半小夜曲-李克勤.lrc", "cover": "/assets/audio/月半小夜曲-李克勤.jpg" }, { "name": "海阔天空", "artist": "Beyond", "url": "/assets/audio/海阔天空-Beyond.mp3", "lrc": "/assets/audio/海阔天空-Beyond.lrc", "cover": "/assets/audio/海阔天空-Beyond.jpg" }, { "name": "红豆", "artist": "王菲", "url": "/assets/audio/红豆-王菲.mp3", "lrc": "/assets/audio/红豆-王菲.lrc", "cover": "/assets/audio/红豆-王菲.jpg" }, ]
const container = document.getElementById("aplayer") const lrcContainerSelector = `.aplayer-lrc-contents`; let player; let isUserScrolling = false; let scrollTimer = null; let userScrollCheckDelay = 1000; let scrollIntoViewDelay = 800;
const aplayerConfig = { container, autoplay: false, theme: themeColor, loop: "all", order: "list", preload: "auto", volume: 0.7, mutex: true, listFolded: false, listMaxHeight: '200px', lrcType: 3, } let isNormalMode = true const modesWithoutLrc = ['fixed', 'mini'] if (modesWithoutLrc.includes(mode)) { aplayerConfig[mode] = true isNormalMode = false } let audioList = originAudioList; if (!isNormalMode) { audioList = audioList.map((item) => ({ ...item, lrc: '' })) } aplayerConfig.audio = audioList
document.addEventListener("DOMContentLoaded", initAPlayer);
function initAPlayer() { player = new APlayer(aplayerConfig);
if (isNormalMode) { displayLrc() } }
function displayLrc() { if (!player) { return; } const lrcContainer = document.createElement('div') lrcContainer.classList.add('custom-lrc-container') const defaultLrcContainer = document.querySelector('.aplayer-lrc') if (defaultLrcContainer) { lrcContainer.appendChild(defaultLrcContainer) container.appendChild(lrcContainer) }
const lyricContainer = document.querySelector(lrcContainerSelector);
lyricContainer.addEventListener('scroll', throttle(userScrollHandler, userScrollCheckDelay));
player.on('timeupdate', throttle(scrollCurrentLineIntoView, scrollIntoViewDelay)) }
function userScrollHandler() { isUserScrolling = true; if (scrollTimer) clearTimeout(scrollTimer); scrollTimer = setTimeout(() => { isUserScrolling = false; }, userScrollCheckDelay); }
function scrollCurrentLineIntoView() { if (player?.audio.paused || isUserScrolling) { return; }
const lyricContainer = document.querySelector(lrcContainerSelector); const currentLine = document.querySelector('.aplayer-lrc-current'); if (!currentLine) return;
const containerHeight = lyricContainer.clientHeight; const lineHeight = currentLine.offsetHeight;
const lineOffsetTop = currentLine.offsetTop;
let scrollPosition = lineOffsetTop - (containerHeight / 2) + (lineHeight / 2);
if (scrollPosition < 0) { scrollPosition = 0 } lyricContainer.scrollTo({ top: scrollPosition, behavior: "smooth" }) }
function throttle(fn, delay = 400) { let timer = null
return function () { if (timer) { return } timer = setTimeout(() => { fn.apply(this, arguments) timer = null }, delay) }
} </script>
<link rel="stylesheet" href="/css/aplayer.css" />
<% } %>
|