Skip to content

Commit

Permalink
优化歌曲URL检查机制
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed Nov 23, 2024
1 parent dfbe3f6 commit f9f8297
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/renderer/core/useApp/usePlayer/usePreloadNextMusic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,46 @@ import { musicInfo } from '@renderer/store/player/state'
// import { getList } from '@renderer/store/utils'
import { getNextPlayMusicInfo, resetRandomNextMusicInfo } from '@renderer/core/player'
import { getMusicUrl } from '@renderer/core/music'
import { checkUrl } from '@renderer/utils/request'
import { appSetting } from '@renderer/store/setting'

let audio: HTMLAudioElement
const initAudio = () => {
if (audio) return
audio = new Audio()
audio.controls = false
audio.preload = 'auto'
audio.crossOrigin = 'anonymous'
audio.muted = true
audio.volume = 0
audio.autoplay = true
audio.addEventListener('playing', () => {
audio.pause()
})
}
const checkMusicUrl = async(url: string) => {
initAudio()
return new Promise((resolve) => {
const clear = () => {
audio.removeEventListener('error', handleErr)
audio.removeEventListener('canplay', handlePlay)
}
const handleErr = () => {
if (audio?.error?.code !== 1) {
resolve(false)
} else {
resolve(true)
}
}
const handlePlay = () => {
clear()
resolve(true)
}
audio.addEventListener('error', handleErr)
audio.addEventListener('canplay', handlePlay)
audio.src = url
})
}

const preloadMusicInfo = {
isLoading: false,
preProgress: 0,
Expand All @@ -28,9 +65,10 @@ const preloadNextMusicUrl = async(curTime: number) => {
const url = await getMusicUrl({ musicInfo: info.musicInfo }).catch(() => '')
if (url) {
console.log('preload url', url)
const result = await checkUrl(url).then(() => true).catch(() => false)
const result = await checkMusicUrl(url)
if (!result) {
const url = await getMusicUrl({ musicInfo: info.musicInfo, isRefresh: true }).catch(() => '')
void checkMusicUrl(url)
console.log('preload url refresh', url)
}
}
Expand Down

0 comments on commit f9f8297

Please sign in to comment.