Pārlūkot izejas kodu

修复歌单播放异常

chendeben 1 gadu atpakaļ
vecāks
revīzija
d45fb2c737

+ 6 - 4
entry/src/main/ets/common/service/UnifiedPlayerService.ets

@@ -1072,6 +1072,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
   }
 
   async playSongAtIndex(index: number): Promise<void> {
+    console.log("Heanup 开始执行playSongAtIndex方法,播放指定索引的歌:"+index)
     try {
       // 设置手动歌曲切换标志,防止自动播放干扰
       this.isManualSongChange = true;
@@ -1093,15 +1094,16 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
         await this.playlistModel.ensureCurrentSongInHistory();
       }
 
-      await this.startPlayOrResumePlay();
+      // 直接播放新歌曲,而不是恢复播放
+      const newCurrentSong = this.playlistModel.getCurrentSong();
+      await this.playNewSongDirectly(newCurrentSong);
 
       // 更新状态模型中的播放列表状态
       await this.updatePlaylistStateInModel();
 
       // 通知歌曲变化
-      const currentSong = this.playlistModel.getCurrentSong();
-      if (currentSong) {
-        this.stateModel.updateCurrentSong(currentSong);
+      if (newCurrentSong) {
+        this.stateModel.updateCurrentSong(newCurrentSong);
       }
 
       // 同步更新所有桌面卡片状态

+ 74 - 18
entry/src/main/ets/view/LocalMusic.ets

@@ -3477,6 +3477,7 @@ export struct LocalMusic {
         .onClick(() => {
           // ToastUtil.showToast('当前的手机的折叠状态 ='+DisplayUtil.getFoldStatus())
           if (this.modeType === 0) {
+            // 文件夹模式:使用当前文件夹的音乐列表
             if (ArrayUtil.isNotEmpty(this.getCurFileList())) {
               this.doPlay(this.getCurFileList()[0])
             }
@@ -4834,7 +4835,10 @@ export struct LocalMusic {
           this.currentPath = item.filePath
           this.currentTitleCover = $r('app.media.ic_avatar5')
           this.getSortedFiles(this.currentPath)
-
+          
+          // 进入新文件夹后,清空当前播放列表,让下次播放时重新创建
+          // 这样确保播放列表跟随当前文件夹的内容
+          // 注意:不清空 this.songList,保持当前播放状态,只是在下次点击播放时会重新设置
         }
         //进入歌单的时候的滚动位置在顶部
         if (this.isGridMusic) {
@@ -4953,23 +4957,33 @@ export struct LocalMusic {
             this.curIndex = index
           }
         } else {
-          // 点击文件列表中的歌曲,需要判断是否要创建新的播放列表
-          let globalVideoList = Utility.getGlobalList(this.videoLocalList, CommonConstants.TYPE_LOCAL) as VideoItem[];
-          this.curIndex = Utility.getCurIndexFromGlobalList(globalVideoList, item.filePath)
-
-          // 检查当前播放列表是否包含这首歌
-          const currentSongIndex = this.songList.findIndex(song => song.filePath === item.filePath);
-
-          if (currentSongIndex >= 0 && ArrayUtil.isNotEmpty(this.songList)) {
-            // 如果当前播放列表已经包含这首歌,使用现有播放列表
-            this.curIndex = currentSongIndex;
-            this.currentSong = this.songList[this.curIndex];
-          } else {
-            // 如果当前播放列表不包含这首歌,或者没有播放列表,则创建新的
-            this.songList = globalVideoList;
-            this.sonDataSource.pushArrayData(this.songList);
-            this.currentSong = globalVideoList[this.curIndex];
+          // 点击文件列表中的歌曲,根据当前模式创建相应的播放列表
+          let currentPlaylist: VideoItem[] = [];
+          
+          if (this.modeType === 0) {
+            // 文件夹模式:使用当前文件夹的音乐
+            currentPlaylist = Utility.getGlobalList(this.videoLocalList, CommonConstants.TYPE_LOCAL) as VideoItem[];
+          } else if (this.modeType === 1) {
+            // 媒体库模式:使用全部媒体库音乐
+            currentPlaylist = [...this.mediaKuList]; // 使用副本避免意外修改
+          }  else {
+            currentPlaylist = Utility.getGlobalList(this.videoLocalList, CommonConstants.TYPE_LOCAL) as VideoItem[];
+          }
+          
+          if (ArrayUtil.isEmpty(currentPlaylist)) {
+            ToastUtil.showToast('当前列表中没有可播放的音乐');
+            return;
+          }
+          
+          this.curIndex = Utility.getCurIndexFromGlobalList(currentPlaylist, item.filePath);
+          if (this.curIndex < 0) {
+            this.curIndex = 0; // 如果找不到,则播放第一首
           }
+
+          // 总是使用当前上下文的播放列表,确保播放列表跟随当前浏览的内容
+          this.songList = currentPlaylist;
+          this.sonDataSource.pushArrayData(this.songList);
+          this.currentSong = this.songList[this.curIndex];
         }
         this.videoUrl = this.currentSong.filePath
         this.name = this.currentSong.name
@@ -4985,7 +4999,8 @@ export struct LocalMusic {
 
         // 同步播放列表到UnifiedPlayerService
         this.unifiedPlayerService.syncPlaylistToService(this.songList, this.curIndex, this.playType);
-        this.startPlayOrResumePlay()
+        // 强制播放特定歌曲
+        this.playSpecificSong()
         break;
 
     }
@@ -10403,6 +10418,47 @@ export struct LocalMusic {
     this.mDestroyPage = true;
   }
 
+  // 播放特定歌曲 - 强制重新设置播放器
+  private async playSpecificSong() {
+    try {
+      LogUtils.getInstance().LOGI(`Heanup LocalMusic: Playing specific song at index ${this.curIndex}: ${this.currentSong?.name}`);
+      
+      this.startPipLyric();
+      this.mDestroyPage = false;
+      this.animationState = AnimationStatus.Running;
+
+      // 使用UnifiedPlayerService强制播放特定索引的歌曲
+      // 这里不使用startPlayOrResumePlay,而是使用能够强制重新设置播放器的方法
+      await this.unifiedPlayerService.playSongAtIndex(this.curIndex);
+
+      // 同步状态到本地UI
+      const currentState = this.unifiedPlayerService.getCurrentState();
+      if (currentState.currentSong) {
+        this.currentSong = currentState.currentSong;
+        this.videoUrl = this.currentSong.filePath;
+        this.name = this.currentSong.name;
+        this.artist = this.currentSong.artist;
+        this.cover = this.currentSong.pixelMapPath;
+      }
+
+      // 保持原有的UI更新逻辑
+      this.stopProgressTask();
+      this.startProgressTask();
+      this.watchStatus();
+      this.updateLastPlayTimeStr(this.videoUrl);
+
+      // 更新播放状态
+      this.CONTROL_PlayStatus = PlayStatus.PLAY;
+      this.setIsPlaying(true);
+
+      LogUtils.getInstance().LOGI("LocalMusic: playSpecificSong completed via UnifiedPlayerService");
+    } catch (error) {
+      LogUtils.getInstance().LOGI(`LocalMusic playSpecificSong error: ${error}`);
+      ToastUtil.showToast(`播放失败: ${error}`);
+      this.handlePlaybackError();
+    }
+  }
+
   // 设置音量(通过UnifiedPlayerService)
   private setVolumeViaService(volume: number) {
     try {