@@ -138,6 +138,12 @@ const drawVisualizer = () => {
138138const 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 () => {
212219const 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 = () => {
221228const 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 }
0 commit comments