@@ -188,6 +188,7 @@ const handleRestorePlay = async(restorePlayInfo: LX.Player.SavedPlayInfo) => {
188188const handlePlay = ( ) => {
189189 window . lx . isPlayedStop &&= false
190190
191+ resetRandomNextMusicInfo ( )
191192 if ( window . lx . restorePlayInfo ) {
192193 void handleRestorePlay ( window . lx . restorePlayInfo )
193194 window . lx . restorePlayInfo = null
@@ -253,6 +254,104 @@ const handleToggleStop = () => {
253254 } )
254255}
255256
257+ const randomNextMusicInfo = {
258+ info : null as LX . Player . PlayMusicInfo | null ,
259+ index : - 1 ,
260+ }
261+ export const resetRandomNextMusicInfo = ( ) => {
262+ if ( randomNextMusicInfo . info ) {
263+ randomNextMusicInfo . info = null
264+ randomNextMusicInfo . index = - 1
265+ }
266+ }
267+
268+ export const getNextPlayMusicInfo = async ( ) : Promise < LX . Player . PlayMusicInfo | null > => {
269+ if ( tempPlayList . length ) { // 如果稍后播放列表存在歌曲则直接播放改列表的歌曲
270+ const playMusicInfo = tempPlayList [ 0 ]
271+ return playMusicInfo
272+ }
273+
274+ if ( playMusicInfo . musicInfo == null ) return null
275+
276+ if ( randomNextMusicInfo . info ) return randomNextMusicInfo . info
277+
278+ // console.log(playInfo.playerListId)
279+ const currentListId = playInfo . playerListId
280+ if ( ! currentListId ) return null
281+ const currentList = getList ( currentListId )
282+
283+ if ( playedList . length ) { // 移除已播放列表内不存在原列表的歌曲
284+ let currentId : string
285+ if ( playMusicInfo . isTempPlay ) {
286+ const musicInfo = currentList [ playInfo . playerPlayIndex ]
287+ if ( musicInfo ) currentId = musicInfo . id
288+ } else {
289+ currentId = playMusicInfo . musicInfo . id
290+ }
291+ // 从已播放列表移除播放列表已删除的歌曲
292+ let index
293+ for ( index = playedList . findIndex ( m => m . musicInfo . id === currentId ) + 1 ; index < playedList . length ; index ++ ) {
294+ const playMusicInfo = playedList [ index ]
295+ const currentId = playMusicInfo . musicInfo . id
296+ if ( playMusicInfo . listId == currentListId && ! currentList . some ( m => m . id === currentId ) ) {
297+ removePlayedList ( index )
298+ continue
299+ }
300+ break
301+ }
302+
303+ if ( index < playedList . length ) return playedList [ index ]
304+ }
305+ // const isCheckFile = findNum > 2 // 针对下载列表,如果超过两次都碰到无效歌曲,则过滤整个列表内的无效歌曲
306+ let { filteredList, playerIndex } = await filterList ( { // 过滤已播放歌曲
307+ listId : currentListId ,
308+ list : currentList ,
309+ playedList,
310+ playerMusicInfo : currentList [ playInfo . playerPlayIndex ] ,
311+ isNext : true ,
312+ } )
313+
314+ if ( ! filteredList . length ) return null
315+ // let currentIndex: number = filteredList.indexOf(currentList[playInfo.playerPlayIndex])
316+ if ( playerIndex == - 1 && filteredList . length ) playerIndex = 0
317+ let nextIndex = playerIndex
318+
319+ let togglePlayMethod = appSetting [ 'player.togglePlayMethod' ]
320+ switch ( togglePlayMethod ) {
321+ case 'listLoop' :
322+ nextIndex = playerIndex === filteredList . length - 1 ? 0 : playerIndex + 1
323+ break
324+ case 'random' :
325+ nextIndex = getRandom ( 0 , filteredList . length )
326+ break
327+ case 'list' :
328+ nextIndex = playerIndex === filteredList . length - 1 ? - 1 : playerIndex + 1
329+ break
330+ case 'singleLoop' :
331+ break
332+ default :
333+ return null
334+ }
335+ if ( nextIndex < 0 ) return null
336+
337+ const nextPlayMusicInfo = {
338+ musicInfo : filteredList [ nextIndex ] ,
339+ listId : currentListId ,
340+ isTempPlay : false ,
341+ }
342+
343+ if ( togglePlayMethod == 'random' ) {
344+ randomNextMusicInfo . info = nextPlayMusicInfo
345+ randomNextMusicInfo . index = nextIndex
346+ }
347+ return nextPlayMusicInfo
348+ }
349+
350+ const handlePlayNext = ( playMusicInfo : LX . Player . PlayMusicInfo ) => {
351+ pause ( )
352+ setPlayMusicInfo ( playMusicInfo . listId , playMusicInfo . musicInfo , playMusicInfo . isTempPlay )
353+ handlePlay ( )
354+ }
256355/**
257356 * 下一曲
258357 * @param isAutoToggle 是否自动切换
@@ -263,9 +362,7 @@ export const playNext = async(isAutoToggle = false): Promise<void> => {
263362 if ( tempPlayList . length ) { // 如果稍后播放列表存在歌曲则直接播放改列表的歌曲
264363 const playMusicInfo = tempPlayList [ 0 ]
265364 removeTempPlayList ( 0 )
266- pause ( )
267- setPlayMusicInfo ( playMusicInfo . listId , playMusicInfo . musicInfo , playMusicInfo . isTempPlay )
268- handlePlay ( )
365+ handlePlayNext ( playMusicInfo )
269366 console . log ( 'play temp list' )
270367 return
271368 }
@@ -306,14 +403,15 @@ export const playNext = async(isAutoToggle = false): Promise<void> => {
306403 }
307404
308405 if ( index < playedList . length ) {
309- const playMusicInfo = playedList [ index ]
310- pause ( )
311- setPlayMusicInfo ( playMusicInfo . listId , playMusicInfo . musicInfo , playMusicInfo . isTempPlay )
312- handlePlay ( )
406+ handlePlayNext ( playedList [ index ] )
313407 console . log ( 'play played list' )
314408 return
315409 }
316410 }
411+ if ( randomNextMusicInfo . info ) {
412+ handlePlayNext ( randomNextMusicInfo . info )
413+ return
414+ }
317415 // const isCheckFile = findNum > 2 // 针对下载列表,如果超过两次都碰到无效歌曲,则过滤整个列表内的无效歌曲
318416 let { filteredList, playerIndex } = await filterList ( { // 过滤已播放歌曲
319417 listId : currentListId ,
@@ -363,15 +461,11 @@ export const playNext = async(isAutoToggle = false): Promise<void> => {
363461 return
364462 }
365463
366- const nextPlayMusicInfo = {
464+ handlePlayNext ( {
367465 musicInfo : filteredList [ nextIndex ] ,
368466 listId : currentListId ,
369467 isTempPlay : false ,
370- }
371-
372- pause ( )
373- setPlayMusicInfo ( nextPlayMusicInfo . listId , nextPlayMusicInfo . musicInfo )
374- handlePlay ( )
468+ } )
375469}
376470
377471/**
@@ -411,10 +505,7 @@ export const playPrev = async(isAutoToggle = false): Promise<void> => {
411505 }
412506
413507 if ( index > - 1 ) {
414- const playMusicInfo = playedList [ index ]
415- pause ( )
416- setPlayMusicInfo ( playMusicInfo . listId , playMusicInfo . musicInfo , playMusicInfo . isTempPlay )
417- handlePlay ( )
508+ handlePlayNext ( playedList [ index ] )
418509 return
419510 }
420511 }
@@ -462,15 +553,11 @@ export const playPrev = async(isAutoToggle = false): Promise<void> => {
462553 if ( nextIndex < 0 ) return
463554 }
464555
465- const nextPlayMusicInfo = {
556+ handlePlayNext ( {
466557 musicInfo : filteredList [ nextIndex ] ,
467558 listId : currentListId ,
468559 isTempPlay : false ,
469- }
470-
471- pause ( )
472- setPlayMusicInfo ( nextPlayMusicInfo . listId , nextPlayMusicInfo . musicInfo )
473- handlePlay ( )
560+ } )
474561}
475562
476563/**
0 commit comments