Skip to content

Commit

Permalink
优化调用播放模块逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed Apr 1, 2023
1 parent 8677d6a commit cc2f95b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/core/common.ts
Expand Up @@ -14,6 +14,7 @@ import { saveData } from '@/plugins/storage'
import { throttle } from '@/utils/common'
import { saveFontSize, saveViewPrevState } from '@/utils/data'
import { showPactModal as handleShowPactModal } from '@/navigation'
import { hideLyric } from '@/utils/nativeModules/lyricDesktop'


const throttleSaveSetting = throttle(() => {
Expand Down Expand Up @@ -54,6 +55,7 @@ export const exitApp = () => {
Promise.all([
hideDesktopLyric(),
destroyPlayer(),
hideLyric(),
]).finally(() => {
isDestroying = false
utilExitApp()
Expand Down
17 changes: 13 additions & 4 deletions src/plugins/player/playList.ts
Expand Up @@ -126,8 +126,8 @@ export const updateMetaData = async(musicInfo: LX.Player.MusicInfo, isPlay: bool
}
}

export const playMusic = async(musicInfo: LX.Player.PlayMusic, url: string, time: number) => {
// console.log(tracks, time)
const handlePlayMusic = async(musicInfo: LX.Player.PlayMusic, url: string, time: number) => {
// console.log(tracks, time)
const tracks = buildTracks(musicInfo, url)
const track = tracks[0]
// await updateMusicInfo(track)
Expand All @@ -144,8 +144,8 @@ export const playMusic = async(musicInfo: LX.Player.PlayMusic, url: string, time
// let startupAutoPlay = settingState.setting['player.startupAutoPlay']
global.lx.restorePlayInfo = null

// TODO startupAutoPlay
// if (startupAutoPlay) store.dispatch(playerAction.playMusic())
// TODO startupAutoPlay
// if (startupAutoPlay) store.dispatch(playerAction.playMusic())
} else {
await TrackPlayer.play()
}
Expand All @@ -162,6 +162,15 @@ export const playMusic = async(musicInfo: LX.Player.PlayMusic, url: string, time
void TrackPlayer.remove(Array(queue.length - 2).fill(null).map((_, i) => i)).then(() => list.splice(0, list.length - 2))
}
}
let playPromise = Promise.resolve()
let actionId = Math.random()
export const playMusic = (musicInfo: LX.Player.PlayMusic, url: string, time: number) => {
const id = actionId = Math.random()
playPromise.finally(() => {
if (id != actionId) return
playPromise = handlePlayMusic(musicInfo, url, time)
})
}

// let musicId = null
// let duration = 0
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/player/utils.ts
Expand Up @@ -145,7 +145,7 @@ const playMusic = ((fn: (musicInfo: LX.Player.PlayMusic, url: string, time: numb
}
}
})((musicInfo, url, time) => {
void handlePlayMusic(musicInfo, url, time)
handlePlayMusic(musicInfo, url, time)
})

export const setResource = (musicInfo: LX.Player.PlayMusic, url: string, duration?: number) => {
Expand Down

0 comments on commit cc2f95b

Please sign in to comment.