Просмотр исходного кода

修复随机模式下播放上一首的问题

chendeben 1 год назад
Родитель
Сommit
b5c0bb5730

+ 71 - 4
entry/src/main/ets/common/service/PlaylistModel.ets

@@ -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}`);
+  }
+
   /**
    * 获取播放历史记录
    */

+ 18 - 1
entry/src/main/ets/common/service/UnifiedPlayerService.ets

@@ -554,6 +554,12 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
   private async setupAndStartPlayback(song: VideoItem): Promise<void> {
     console.log(`Heanup2 UnifiedPlayerService: 设置播放器并开始播放: ${song.name}`);
 
+    // 在随机播放模式下,确保播放历史被正确维护
+    const playMode = this.stateModel.getState().playMode;
+    if (playMode === PlayMode.RANDOM) {
+      this.playlistModel.ensureCurrentSongInHistory();
+    }
+
     // 设置播放源和配置
     await this.setupPlayerForSong(song);
 
@@ -593,6 +599,12 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
       }
     }
 
+    // 在随机播放模式下,确保播放历史被正确维护
+    const playMode = this.stateModel.getState().playMode;
+    if (playMode === PlayMode.RANDOM) {
+      this.playlistModel.ensureCurrentSongInHistory();
+    }
+
     // 设置播放源和配置
     await this.setupPlayerForSong(song);
 
@@ -2251,12 +2263,17 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
       const totalCount = this.playlistModel.getTotalCount();
       const playMode = this.stateModel.getState().playMode;
 
+      // 在随机播放模式下,确保当前歌曲在播放历史中
+      if (playMode === PlayMode.RANDOM) {
+        this.playlistModel.ensureCurrentSongInHistory();
+      }
+
       const hasNext = this.playlistModel.hasNext(playMode);
       const hasPrevious = this.playlistModel.hasPrevious(playMode);
 
       this.stateModel.updatePlaylistState(hasNext, hasPrevious, totalCount);
 
-      LogUtils.getInstance().LOGI(`UnifiedPlayerService: Playlist state updated - hasNext=${hasNext}, hasPrevious=${hasPrevious}, totalCount=${totalCount}`);
+      LogUtils.getInstance().LOGI(`UnifiedPlayerService: Playlist state updated - hasNext=${hasNext}, hasPrevious=${hasPrevious}, totalCount=${totalCount}, playMode=${playMode}`);
     } catch (error) {
       LogUtils.getInstance().LOGI(`UnifiedPlayerService updatePlaylistStateInModel error: ${error}`);
     }

+ 6 - 10
entry/src/main/ets/common/widget/GlobalWidgetService.ets

@@ -138,21 +138,17 @@ export class GlobalWidgetService {
       }
     };
     
+    // 注意:这里不再简单地基于索引修复按钮状态
+    // 因为按钮状态应该由 UnifiedPlayerService 中的播放模式逻辑正确计算
+    // 在随机播放模式下,按钮状态取决于播放历史记录,而不是简单的索引位置
     if (data.playlist.totalCount > 1) {
-      const correctHasNext = data.playlist.currentIndex < data.playlist.totalCount - 1;
-      const correctHasPrevious = data.playlist.currentIndex > 0;
-      
-      // 更新按钮状态
-      correctedData.playlist.hasNext = correctHasNext;
-      correctedData.playlist.hasPrevious = correctHasPrevious;
-      
-      if (correctHasNext !== data.playlist.hasNext || correctHasPrevious !== data.playlist.hasPrevious) {
-        hilog.info(0x0000, TAG, `Fixed button states in GlobalWidgetService: hasNext=${correctHasNext}, hasPrevious=${correctHasPrevious}`);
-      }
+      // 保持从 UnifiedPlayerService 传来的状态,这些状态已经考虑了播放模式
+      hilog.info(0x0000, TAG, `Keeping original button states from UnifiedPlayerService: hasNext=${data.playlist.hasNext}, hasPrevious=${data.playlist.hasPrevious}`);
     } else {
       // 单首歌或无歌曲时,禁用所有按钮
       correctedData.playlist.hasNext = false;
       correctedData.playlist.hasPrevious = false;
+      hilog.info(0x0000, TAG, `Single song or empty playlist, disabled all navigation buttons`);
     }
     
     return correctedData;