From e32f6ef871d536bb9d6f32535d80231aa643e5ac Mon Sep 17 00:00:00 2001 From: QiuLiang-99 <1297829693@qq.com> Date: Sat, 3 May 2025 17:14:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E5=AF=B9=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E5=88=97=E8=A1=A8=E7=9A=84=E9=9A=8F=E6=9C=BA=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/core/player/action.ts | 117 +++++++++++++----- .../utils/compositions/useNextTogglePlay.ts | 15 +++ src/renderer/views/List/MusicList/usePlay.js | 31 +++-- 3 files changed, 119 insertions(+), 44 deletions(-) diff --git a/src/renderer/core/player/action.ts b/src/renderer/core/player/action.ts index 189b950a..8e6f9527 100644 --- a/src/renderer/core/player/action.ts +++ b/src/renderer/core/player/action.ts @@ -1,5 +1,5 @@ import { isEmpty, setPause, setPlay, setResource, setStop } from '@renderer/plugins/player' -import { isPlay, playedList, playInfo, playMusicInfo, tempPlayList, musicInfo as _musicInfo, currentPlayIndex } from '@renderer/store/player/state' +import { isPlay, playedList, playInfo, playMusicInfo, tempPlayList, musicInfo as _musicInfo, currentPlayIndex, currentPlaybackOrder } from '@renderer/store/player/state' import { getList, clearPlayedList, @@ -148,6 +148,7 @@ export const setMusicUrl = (musicInfo: LX.Music.MusicInfo | LX.Download.ListItem // 恢复上次播放的状态 const handleRestorePlay = async(restorePlayInfo: LX.Player.SavedPlayInfo) => { + // todo 恢复软件关闭时tempplaylist的状态 const musicInfo = playMusicInfo.musicInfo if (!musicInfo) return @@ -364,7 +365,7 @@ export const getNextPlayMusicInfo = async(): Promise { +export const handlePlayNext = (playMusicInfo: LX.Player.PlayMusicInfo) => { // pause() setPlayMusicInfo(playMusicInfo.listId, playMusicInfo.musicInfo, playMusicInfo.isTempPlay)//todo 设置播放音乐 handlePlay() @@ -376,9 +377,35 @@ const handlePlayNext = (playMusicInfo: LX.Player.PlayMusicInfo) => { */ export const playNext = async(isAutoToggle = false): Promise => { console.log('skip next', isAutoToggle) - //todo 需要修改逻辑,不要删除 + let togglePlayMethod = appSetting['player.togglePlayMethod'] + if (!isAutoToggle) { + switch (togglePlayMethod) { + case 'list': + case 'singleLoop': + case 'none': + togglePlayMethod = 'listLoop' + } + } + switch (togglePlayMethod) { + case 'listLoop': + currentPlayIndex.value++ + // bug 实际上不是列表循环... + break + case 'random': + currentPlayIndex.value++ + break + case 'list': + currentPlayIndex.value++ + break + case 'singleLoop': + break + default: + currentPlayIndex.value = -1 + console.log('stop toggle play', togglePlayMethod, isAutoToggle) + return + } if (tempPlayList.length) { // 如果稍后播放列表存在歌曲则直接播放改列表的歌曲 - const playMusicInfo = tempPlayList[++(currentPlayIndex.value) ] + const playMusicInfo = tempPlayList[currentPlaybackOrder.value[currentPlayIndex.value]] // removeTempPlayList(0) handlePlayNext(playMusicInfo) console.log('play temp list') @@ -448,32 +475,32 @@ export const playNext = async(isAutoToggle = false): Promise => { if (playerIndex == -1 && filteredList.length) playerIndex = 0 let nextIndex = playerIndex - let togglePlayMethod = appSetting['player.togglePlayMethod'] - if (!isAutoToggle) { - switch (togglePlayMethod) { - case 'list': - case 'singleLoop': - case 'none': - togglePlayMethod = 'listLoop' - } - } - switch (togglePlayMethod) { - case 'listLoop': - nextIndex = playerIndex === filteredList.length - 1 ? 0 : playerIndex + 1 - break - case 'random': - nextIndex = getRandom(0, filteredList.length) - break - case 'list': - nextIndex = playerIndex === filteredList.length - 1 ? -1 : playerIndex + 1 - break - case 'singleLoop': - break - default: - nextIndex = -1 - console.log('stop toggle play', togglePlayMethod, isAutoToggle) - return - } + // let togglePlayMethod = appSetting['player.togglePlayMethod'] + // if (!isAutoToggle) { + // switch (togglePlayMethod) { + // case 'list': + // case 'singleLoop': + // case 'none': + // togglePlayMethod = 'listLoop' + // } + // } + // switch (togglePlayMethod) { + // case 'listLoop': + // nextIndex = playerIndex === filteredList.length - 1 ? 0 : playerIndex + 1 + // break + // case 'random': + // nextIndex = getRandom(0, filteredList.length) + // break + // case 'list': + // nextIndex = playerIndex === filteredList.length - 1 ? -1 : playerIndex + 1 + // break + // case 'singleLoop': + // break + // default: + // nextIndex = -1 + // console.log('stop toggle play', togglePlayMethod, isAutoToggle) + // return + // } if (nextIndex < 0) { console.log('next index empty') return @@ -490,8 +517,36 @@ export const playNext = async(isAutoToggle = false): Promise => { * 上一曲 */ export const playPrev = async(isAutoToggle = false): Promise => { + // todo 重复代码块,可以提取 + let togglePlayMethod = appSetting['player.togglePlayMethod'] + if (!isAutoToggle) { + switch (togglePlayMethod) { + case 'list': + case 'singleLoop': + case 'none': + togglePlayMethod = 'listLoop' + } + } + switch (togglePlayMethod) { + case 'listLoop': + currentPlayIndex.value-- + // bug 实际上不是列表循环... + break + case 'random': + currentPlayIndex.value-- + break + case 'list': + currentPlayIndex.value-- + break + case 'singleLoop': + break + default: + currentPlayIndex.value = -1 + console.log('stop toggle play', togglePlayMethod, isAutoToggle) + return + } if (currentPlayIndex.value > 0) { // 如果稍后播放列表存在歌曲则直接播放改列表的歌曲 - const playMusicInfo = tempPlayList[--(currentPlayIndex.value) ] + const playMusicInfo = tempPlayList[currentPlaybackOrder.value[currentPlayIndex.value]] // removeTempPlayList(0) handlePlayNext(playMusicInfo) console.log('play temp list') diff --git a/src/renderer/utils/compositions/useNextTogglePlay.ts b/src/renderer/utils/compositions/useNextTogglePlay.ts index 37047eae..00803316 100644 --- a/src/renderer/utils/compositions/useNextTogglePlay.ts +++ b/src/renderer/utils/compositions/useNextTogglePlay.ts @@ -3,6 +3,7 @@ import { computed, } from '@common/utils/vueTools' import { useI18n } from '@renderer/plugins/i18n' +import { tempPlayList, currentPlayIndex, currentPlaybackOrder } from '@renderer/store/player/state' // const playNextModes = [ // 'listLoop', @@ -28,6 +29,20 @@ export default () => { if (mode == appSetting['player.togglePlayMethod']) return // let index = playNextModes.indexOf(appSetting['player.togglePlayMethod']) // if (++index >= playNextModes.length) index = 0 + const N = tempPlayList.length + const prevPlayIndex = currentPlaybackOrder.value[currentPlayIndex.value] + if (mode === 'random') { + const allIndexes = Array.from({ length: N }, (_, i) => i).filter(i => i !== prevPlayIndex) + for (let i = allIndexes.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)) + ;[allIndexes[i], allIndexes[j]] = [allIndexes[j], allIndexes[i]] + } + currentPlaybackOrder.value = [prevPlayIndex, ...allIndexes] + } else { + currentPlaybackOrder.value = Array.from({ length: N }, (_, i) => i) + currentPlayIndex.value = prevPlayIndex + } + // todo 因为只有此函数会修改播放类别,所以直接在这里应用修改效果 setTogglePlayMode(mode) } diff --git a/src/renderer/views/List/MusicList/usePlay.js b/src/renderer/views/List/MusicList/usePlay.js index a24d73b8..fa3e300f 100644 --- a/src/renderer/views/List/MusicList/usePlay.js +++ b/src/renderer/views/List/MusicList/usePlay.js @@ -1,6 +1,6 @@ import { addTempPlayList, clearTempPlayeList, getList } from '@renderer/store/player/action' import { tempPlayList, currentPlayIndex, currentPlaybackOrder } from '@renderer/store/player/state' -import { playNext } from '@renderer/core/player' +import { handlePlayNext } from '@renderer/core/player' import { appSetting } from '@renderer/store/setting' // import { arrShuffle } from '@common/utils/common' // setup 函数或组件初始化中 @@ -11,24 +11,29 @@ export default ({ props, selectedList, list, removeAllSelect }) => { let clickIndex = -1 const handlePlayMusic = (index) => { - currentPlayIndex.value = index - // todo 播放选中的歌曲 - // todo 修改播放歌曲逻辑 - // playList(props.listId, index) - // handlePlayMusicLater(index, true) - // todo 歌单本身就有乱序排序功能 - if (tempPlayList.length > 0 ) {// 双击操作会切换到当前歌单 + currentPlayIndex.value = 0 + if (tempPlayList.length > 0) { // 双击操作会切换到当前歌单 clearTempPlayeList() } const currentchooselist = getList(props.listId) - for(let music of currentchooselist) { + for (let music of currentchooselist) { addTempPlayList([{ listId: props.listId, musicInfo: music }]) } - if (appSetting['player.togglePlayMethod'] == 'random') { - // todo 随机播放 - // arrShuffle(tempPlayList) + // todo 重复代码块,可提取 + const N = tempPlayList.length + if (appSetting['player.togglePlayMethod'] === 'random') { + const allIndexes = Array.from({ length: N }, (_, i) => i).filter(i => i !== index) + for (let i = allIndexes.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)) + ;[allIndexes[i], allIndexes[j]] = [allIndexes[j], allIndexes[i]] + } + currentPlaybackOrder.value = [index, ...allIndexes] + } else { + currentPlaybackOrder.value = Array.from({ length: N }, (_, i) => i) + currentPlayIndex.value = index } - playNext() + const playMusicInfo = tempPlayList[currentPlaybackOrder.value[currentPlayIndex.value]] + handlePlayNext(playMusicInfo) } const handlePlayMusicLater = (index, single) => {