@@ -188,6 +188,7 @@ const handleRestorePlay = async(restorePlayInfo: LX.Player.SavedPlayInfo) => {
188
188
const handlePlay = ( ) => {
189
189
window . lx . isPlayedStop &&= false
190
190
191
+ resetRandomNextMusicInfo ( )
191
192
if ( window . lx . restorePlayInfo ) {
192
193
void handleRestorePlay ( window . lx . restorePlayInfo )
193
194
window . lx . restorePlayInfo = null
@@ -253,6 +254,104 @@ const handleToggleStop = () => {
253
254
} )
254
255
}
255
256
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
+ }
256
355
/**
257
356
* 下一曲
258
357
* @param isAutoToggle 是否自动切换
@@ -263,9 +362,7 @@ export const playNext = async(isAutoToggle = false): Promise<void> => {
263
362
if ( tempPlayList . length ) { // 如果稍后播放列表存在歌曲则直接播放改列表的歌曲
264
363
const playMusicInfo = tempPlayList [ 0 ]
265
364
removeTempPlayList ( 0 )
266
- pause ( )
267
- setPlayMusicInfo ( playMusicInfo . listId , playMusicInfo . musicInfo , playMusicInfo . isTempPlay )
268
- handlePlay ( )
365
+ handlePlayNext ( playMusicInfo )
269
366
console . log ( 'play temp list' )
270
367
return
271
368
}
@@ -306,14 +403,15 @@ export const playNext = async(isAutoToggle = false): Promise<void> => {
306
403
}
307
404
308
405
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 ] )
313
407
console . log ( 'play played list' )
314
408
return
315
409
}
316
410
}
411
+ if ( randomNextMusicInfo . info ) {
412
+ handlePlayNext ( randomNextMusicInfo . info )
413
+ return
414
+ }
317
415
// const isCheckFile = findNum > 2 // 针对下载列表,如果超过两次都碰到无效歌曲,则过滤整个列表内的无效歌曲
318
416
let { filteredList, playerIndex } = await filterList ( { // 过滤已播放歌曲
319
417
listId : currentListId ,
@@ -363,15 +461,11 @@ export const playNext = async(isAutoToggle = false): Promise<void> => {
363
461
return
364
462
}
365
463
366
- const nextPlayMusicInfo = {
464
+ handlePlayNext ( {
367
465
musicInfo : filteredList [ nextIndex ] ,
368
466
listId : currentListId ,
369
467
isTempPlay : false ,
370
- }
371
-
372
- pause ( )
373
- setPlayMusicInfo ( nextPlayMusicInfo . listId , nextPlayMusicInfo . musicInfo )
374
- handlePlay ( )
468
+ } )
375
469
}
376
470
377
471
/**
@@ -411,10 +505,7 @@ export const playPrev = async(isAutoToggle = false): Promise<void> => {
411
505
}
412
506
413
507
if ( index > - 1 ) {
414
- const playMusicInfo = playedList [ index ]
415
- pause ( )
416
- setPlayMusicInfo ( playMusicInfo . listId , playMusicInfo . musicInfo , playMusicInfo . isTempPlay )
417
- handlePlay ( )
508
+ handlePlayNext ( playedList [ index ] )
418
509
return
419
510
}
420
511
}
@@ -462,15 +553,11 @@ export const playPrev = async(isAutoToggle = false): Promise<void> => {
462
553
if ( nextIndex < 0 ) return
463
554
}
464
555
465
- const nextPlayMusicInfo = {
556
+ handlePlayNext ( {
466
557
musicInfo : filteredList [ nextIndex ] ,
467
558
listId : currentListId ,
468
559
isTempPlay : false ,
469
- }
470
-
471
- pause ( )
472
- setPlayMusicInfo ( nextPlayMusicInfo . listId , nextPlayMusicInfo . musicInfo )
473
- handlePlay ( )
560
+ } )
474
561
}
475
562
476
563
/**
0 commit comments