Skip to content

Commit d0c1e72

Browse files
committed
Optimize music and image loading logic
1 parent d4f9e16 commit d0c1e72

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

frontend/src/components/MusicPlayer.vue

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ const drawVisualizer = () => {
138138
const togglePlay = async () => {
139139
if (!audioRef.value) return
140140
141+
// 首次播放时才加载音频文件
142+
if (!audioRef.value.src) {
143+
audioRef.value.src = currentSong.value.url
144+
audioRef.value.load()
145+
}
146+
141147
if (!audioContext) {
142148
initAudioContext()
143149
}
@@ -179,9 +185,10 @@ const formatTime = (seconds) => {
179185
return `${mins}:${secs.toString().padStart(2, '0')}`
180186
}
181187
182-
const loadSong = (song) => {
188+
const loadSong = (song, autoLoad = false) => {
183189
currentSong.value = song
184-
if (audioRef.value) {
190+
if (audioRef.value && autoLoad) {
191+
// 只有在需要时才加载音频文件
185192
audioRef.value.src = song.url
186193
audioRef.value.load()
187194
}
@@ -196,7 +203,7 @@ const loadPlaylist = async () => {
196203
197204
if (data.length > 0) {
198205
currentIndex.value = 0
199-
loadSong(data[0])
206+
loadSong(data[0], false) // 不自动加载音频文件
200207
}
201208
} catch (error) {
202209
console.error('加载播放列表失败:', error)
@@ -212,7 +219,7 @@ const loadPlaylist = async () => {
212219
const nextSong = () => {
213220
if (playlist.value.length === 0) return
214221
currentIndex.value = (currentIndex.value + 1) % playlist.value.length
215-
loadSong(playlist.value[currentIndex.value])
222+
loadSong(playlist.value[currentIndex.value], true) // 切歌时加载
216223
if (isPlaying.value) {
217224
setTimeout(() => audioRef.value.play(), 100)
218225
}
@@ -221,7 +228,7 @@ const nextSong = () => {
221228
const prevSong = () => {
222229
if (playlist.value.length === 0) return
223230
currentIndex.value = (currentIndex.value - 1 + playlist.value.length) % playlist.value.length
224-
loadSong(playlist.value[currentIndex.value])
231+
loadSong(playlist.value[currentIndex.value], true)
225232
if (isPlaying.value) {
226233
setTimeout(() => audioRef.value.play(), 100)
227234
}

frontend/src/components/cards/LinkCard.vue

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,23 @@ const thumbnailUrl = computed(() => {
6161
return props.link.icon
6262
})
6363
64-
// 图片加载完成后,替换为高清图
64+
// 图片加载完成后,等待页面完全加载再替换为高清图
6565
const onImageLoad = (e) => {
6666
if (imageLoaded.value) return
6767
imageLoaded.value = true
6868
69+
if (document.readyState === 'complete') {
70+
loadHighResImage(e.target)
71+
} else {
72+
window.addEventListener('load', () => {
73+
loadHighResImage(e.target)
74+
}, { once: true })
75+
}
76+
}
77+
78+
// 加载高清图
79+
const loadHighResImage = (img) => {
6980
setTimeout(() => {
70-
const img = e.target
7181
const hdSrc = img.getAttribute('data-src')
7282
if (hdSrc && hdSrc !== img.src) {
7383
const hdImg = new Image()
@@ -76,7 +86,7 @@ const onImageLoad = (e) => {
7686
}
7787
hdImg.src = hdSrc
7888
}
79-
}, 100)
89+
}, 500)
8090
}
8191
8292
// 图片加载失败时的处理

0 commit comments

Comments
 (0)