-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
202 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { getMusicUrl } from '@/core/music' | ||
import { getNextPlayMusicInfo, resetRandomNextMusicInfo } from '@/core/player/player' | ||
import { checkUrl } from '@/utils/request' | ||
import playerState from '@/store/player/state' | ||
|
||
|
||
const preloadMusicInfo = { | ||
isLoading: false, | ||
preProgress: 0, | ||
info: null as LX.Player.PlayMusicInfo | null, | ||
} | ||
const resetPreloadInfo = () => { | ||
preloadMusicInfo.preProgress = 0 | ||
preloadMusicInfo.info = null | ||
preloadMusicInfo.isLoading = false | ||
} | ||
const preloadNextMusicUrl = async(curTime: number) => { | ||
if (preloadMusicInfo.isLoading || curTime - preloadMusicInfo.preProgress < 3) return | ||
preloadMusicInfo.isLoading = true | ||
console.log('preload next music url') | ||
const info = await getNextPlayMusicInfo() | ||
if (info) { | ||
preloadMusicInfo.info = info | ||
const url = await getMusicUrl({ musicInfo: info.musicInfo }).catch(() => '') | ||
if (url) { | ||
console.log('preload url', url) | ||
const result = await checkUrl(url).then(() => true).catch(() => false) | ||
if (!result) { | ||
const url = await getMusicUrl({ musicInfo: info.musicInfo, isRefresh: true }).catch(() => '') | ||
console.log('preload url refresh', url) | ||
} | ||
} | ||
} | ||
preloadMusicInfo.isLoading = false | ||
} | ||
|
||
export default () => { | ||
const setProgress = (time: number) => { | ||
if (!playerState.musicInfo.id) return | ||
preloadMusicInfo.preProgress = time | ||
} | ||
|
||
const handleSetPlayInfo = () => { | ||
resetPreloadInfo() | ||
} | ||
|
||
const handleConfigUpdated: typeof global.state_event.configUpdated = (keys, settings) => { | ||
if (!keys.includes('player.togglePlayMethod')) return | ||
if (!preloadMusicInfo.info || preloadMusicInfo.info.isTempPlay) return | ||
resetRandomNextMusicInfo() | ||
preloadMusicInfo.info = null | ||
preloadMusicInfo.preProgress = playerState.progress.nowPlayTime | ||
} | ||
|
||
const handlePlayProgressChanged: typeof global.state_event.playProgressChanged = (progress) => { | ||
const duration = progress.maxPlayTime | ||
if (duration > 10 && duration - progress.nowPlayTime < 10 && !preloadMusicInfo.info) { | ||
void preloadNextMusicUrl(progress.nowPlayTime) | ||
} | ||
} | ||
|
||
global.app_event.on('setProgress', setProgress) | ||
global.app_event.on('musicToggled', handleSetPlayInfo) | ||
global.state_event.on('configUpdated', handleConfigUpdated) | ||
global.state_event.on('playProgressChanged', handlePlayProgressChanged) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters