|
|
@@ -266,14 +266,26 @@ export class PlaylistModel {
|
|
|
|
|
|
switch (playMode) {
|
|
|
case PlayMode.SINGLE_REPEAT:
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.hasPrevious: SINGLE_REPEAT mode, returning true`);
|
|
|
return true; // 单曲循环总是有上一首(自己)
|
|
|
case PlayMode.SEQUENCE:
|
|
|
- return this.currentIndex > 0;
|
|
|
+ const seqResult = this.currentIndex > 0;
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.hasPrevious: SEQUENCE mode, currentIndex=${this.currentIndex}, result=${seqResult}`);
|
|
|
+ return seqResult;
|
|
|
case PlayMode.NORMAL:
|
|
|
- return this.currentIndex > 0;
|
|
|
+ const normalResult = this.currentIndex > 0;
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.hasPrevious: NORMAL mode, currentIndex=${this.currentIndex}, result=${normalResult}`);
|
|
|
+ return normalResult;
|
|
|
case PlayMode.RANDOM:
|
|
|
- return this.playHistory.length > 1; // 有播放历史记录
|
|
|
+ // 随机模式下:有播放历史记录,或者当前播放的不是第一首(作为备选)
|
|
|
+ const historyLength = this.playHistory.length;
|
|
|
+ const hasHistory = historyLength > 1;
|
|
|
+ const hasBackup = historyLength === 1 && this.songs.length > 1;
|
|
|
+ const randomResult = hasHistory || hasBackup;
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.hasPrevious: RANDOM mode, historyLength=${historyLength}, hasHistory=${hasHistory}, hasBackup=${hasBackup}, result=${randomResult}`);
|
|
|
+ return randomResult;
|
|
|
default:
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.hasPrevious: Unknown mode, returning false`);
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -475,10 +487,37 @@ export class PlaylistModel {
|
|
|
* 获取随机播放模式下的上一首
|
|
|
*/
|
|
|
private getRandomPrevious(): VideoItem | null {
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.getRandomPrevious: playHistory.length=${this.playHistory.length}, songs.length=${this.songs.length}, currentIndex=${this.currentIndex}`);
|
|
|
+
|
|
|
+ // 如果有足够的播放历史记录,从历史记录获取
|
|
|
if (this.playHistory.length >= 2) {
|
|
|
// 返回历史记录的第二首(第一首是当前播放的)
|
|
|
- return this.playHistory[this.playHistory.length - 2];
|
|
|
+ const prevSong = this.playHistory[this.playHistory.length - 2];
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.getRandomPrevious: 从播放历史获取上一首: ${prevSong.name}`);
|
|
|
+ return prevSong;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果历史记录不足,但有多首歌曲,随机选择一首作为"上一首"
|
|
|
+ if (this.songs.length > 1) {
|
|
|
+ LogUtils.getInstance().LOGI('PlaylistModel.getRandomPrevious: 随机模式播放历史不足,随机选择上一首');
|
|
|
+
|
|
|
+ // 排除当前播放的歌曲,从其他歌曲中随机选择
|
|
|
+ const availableIndices: number[] = [];
|
|
|
+ for (let i = 0; i < this.songs.length; i++) {
|
|
|
+ if (i !== this.currentIndex) {
|
|
|
+ availableIndices.push(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (availableIndices.length > 0) {
|
|
|
+ const randomIndex = availableIndices[Math.floor(Math.random() * availableIndices.length)];
|
|
|
+ const randomSong = this.songs[randomIndex];
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.getRandomPrevious: 随机选择歌曲索引 ${randomIndex}: ${randomSong.name}`);
|
|
|
+ return randomSong;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ LogUtils.getInstance().LOGI('PlaylistModel.getRandomPrevious: 无可用的上一首歌曲');
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@@ -493,6 +532,34 @@ export class PlaylistModel {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 确保当前歌曲在播放历史中(用于随机播放模式)
|
|
|
+ */
|
|
|
+ ensureCurrentSongInHistory(): void {
|
|
|
+ const currentSong = this.getCurrentSong();
|
|
|
+ if (!currentSong) {
|
|
|
+ LogUtils.getInstance().LOGI('PlaylistModel.ensureCurrentSongInHistory: 当前没有播放歌曲');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.playHistory.length === 0) {
|
|
|
+ // 如果播放历史为空,添加当前歌曲
|
|
|
+ this.addToPlayHistory(currentSong);
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.ensureCurrentSongInHistory: 初始化播放历史,添加当前歌曲: ${currentSong.name}`);
|
|
|
+ } else {
|
|
|
+ // 检查最后一首是否是当前歌曲,如果不是则添加
|
|
|
+ const lastSong = this.playHistory[this.playHistory.length - 1];
|
|
|
+ if (lastSong.filePath !== currentSong.filePath) {
|
|
|
+ this.addToPlayHistory(currentSong);
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.ensureCurrentSongInHistory: 更新播放历史,添加当前歌曲: ${currentSong.name}`);
|
|
|
+ } else {
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.ensureCurrentSongInHistory: 当前歌曲已在播放历史中: ${currentSong.name}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LogUtils.getInstance().LOGI(`PlaylistModel.ensureCurrentSongInHistory: 播放历史长度: ${this.playHistory.length}`);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取播放历史记录
|
|
|
*/
|