|
|
@@ -547,6 +547,12 @@ export class PlaylistModel {
|
|
|
private async getRandomPrevious(): Promise<VideoItem | null> {
|
|
|
LogUtils.getInstance().LOGI(`PlaylistModel.getRandomPrevious: Starting, cacheLength=${this.playHistory.length}, songsLength=${this.songs.length}, currentIndex=${this.currentIndex}`);
|
|
|
|
|
|
+ const currentSong = this.getCurrentSong();
|
|
|
+ if (!currentSong) {
|
|
|
+ LogUtils.getInstance().LOGI('PlaylistModel.getRandomPrevious: 当前没有播放歌曲');
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
// 优先使用内存缓存,提高性能
|
|
|
let historyToUse = this.playHistory;
|
|
|
|
|
|
@@ -560,17 +566,28 @@ export class PlaylistModel {
|
|
|
|
|
|
// 随机播放模式下,上一首必须严格从播放历史记录中获取
|
|
|
if (historyToUse.length >= 2) {
|
|
|
+ // 关键修复:需要跳过当前播放的歌曲,找到真正的上一首
|
|
|
+ // 播放历史应该是按播放时间倒序排列的(最近播放的在前面)
|
|
|
for (let i = 0; i < historyToUse.length; i++) {
|
|
|
const prevSong = historyToUse[i];
|
|
|
+
|
|
|
+ // 跳过当前正在播放的歌曲
|
|
|
+ if (prevSong.filePath === currentSong.filePath) {
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.getRandomPrevious: 跳过当前歌曲: ${prevSong.name}`);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
// 在当前播放列表中查找这首歌曲
|
|
|
const songIndex = this.songs.findIndex(song => song.filePath === prevSong.filePath);
|
|
|
if (songIndex !== -1) {
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.getRandomPrevious: 找到上一首歌曲: ${prevSong.name}, index: ${songIndex}`);
|
|
|
return this.songs[songIndex];
|
|
|
+ } else {
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.getRandomPrevious: 历史歌曲不在当前播放列表中: ${prevSong.name}`);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- LogUtils.getInstance().LOGI('PlaylistModel.getRandomPrevious: 历史记录中没有找到在当前播放列表中的歌曲');
|
|
|
+ LogUtils.getInstance().LOGI('PlaylistModel.getRandomPrevious: 历史记录中没有找到可用的上一首歌曲(排除当前歌曲后)');
|
|
|
return null;
|
|
|
}
|
|
|
|