瀏覽代碼

歌单相关

chendeben 10 月之前
父節點
當前提交
49d81469e5
共有 2 個文件被更改,包括 56 次插入18 次删除
  1. 52 16
      entry/src/main/ets/pages/PlaylistDetailPage.ets
  2. 4 2
      entry/src/main/ets/view/LocalMusic.ets

+ 52 - 16
entry/src/main/ets/pages/PlaylistDetailPage.ets

@@ -70,18 +70,21 @@ export struct PlaylistDetailPage {
   async loadPlaylistDetail() {
     try {
       this.isLoading = true
-      
+
       if (!this.playlist) {
         ToastUtil.showToast('歌单不存在')
         router.back()
         return
       }
-      
+
       // 加载歌单歌曲
       const playlistSongs = await this.playlistTable.queryPlaylistSongs(this.playlistId)
-      
+
       // 将 PlaylistSong 转换为 VideoItem
       this.songList = await this.convertPlaylistSongsToVideoItems(playlistSongs)
+
+      // 歌单加载完成后,加载当前播放状态
+      this.loadCurrentPlaybackStatus()
     } catch (error) {
       LogUtil.error('heanup 加载歌单详情失败: ' + error)
       ToastUtil.showToast('加载歌单详情失败')
@@ -165,24 +168,29 @@ export struct PlaylistDetailPage {
           const data = eventData.data as Record<string, Object>
           const oldIsPlaying = this.isPlaying
           const oldCurIndex = this.curIndex
-          
-          // 添加调试日志
 
           this.isPlaying = data['isPlaying'] as boolean
-          let newCurIndex = data['curIndex'] as number
-          
-          LogUtil.info(`heanup 原始播放状态: isPlaying=${this.isPlaying}, curIndex=${newCurIndex}`)
+          const currentFilePath = data['currentFilePath'] as string
 
-          
-          this.curIndex = newCurIndex
+          LogUtil.info(`heanup 原始播放状态: isPlaying=${this.isPlaying}, currentFilePath=${currentFilePath}`)
 
-          LogUtil.info(`heanup 播放状态更新: isPlaying=${oldIsPlaying}->${this.isPlaying}, curIndex=${oldCurIndex}->${this.curIndex}`)
-          
-          // 检查当前播放的歌曲是否在当前歌单中
-          if (this.curIndex >= 0 && this.curIndex < this.songList.length) {
-            const currentSong = this.songList[this.curIndex]
-            LogUtil.info(`heanup 当前播放歌曲: ${currentSong.name}, 文件路径: ${currentSong.filePath}`)
+          // 通过filePath在当前歌单中查找对应的索引
+          if (currentFilePath) {
+            const matchedIndex = this.songList.findIndex(song => song.filePath === currentFilePath)
+            if (matchedIndex !== -1) {
+              this.curIndex = matchedIndex
+              LogUtil.info(`heanup 在歌单中找到匹配的歌曲: ${this.songList[matchedIndex].name}, 索引: ${matchedIndex}`)
+            } else {
+              // 当前播放的歌曲不在本歌单中,重置索引
+              this.curIndex = -1
+              LogUtil.info(`heanup 当前播放的歌曲不在本歌单中: ${currentFilePath}`)
+            }
+          } else {
+            this.curIndex = -1
+            LogUtil.info(`heanup 当前没有播放歌曲`)
           }
+
+          LogUtil.info(`heanup 播放状态更新: isPlaying=${oldIsPlaying}->${this.isPlaying}, curIndex=${oldCurIndex}->${this.curIndex}`)
         }
       })
     } catch (error) {
@@ -201,6 +209,34 @@ export struct PlaylistDetailPage {
     }
   }
 
+  /**
+   * 加载当前播放状态
+   */
+  loadCurrentPlaybackStatus() {
+    try {
+      // 从AppStorage获取当前播放的歌曲
+      const currentSong = AppStorage.get<VideoItem>('currentSong')
+      if (currentSong && currentSong.filePath) {
+        LogUtil.info(`heanup 获取到当前播放歌曲: ${currentSong.name}, filePath: ${currentSong.filePath}`)
+
+        // 在歌单中查找匹配的歌曲
+        const matchedIndex = this.songList.findIndex(song => song.filePath === currentSong.filePath)
+        if (matchedIndex !== -1) {
+          this.curIndex = matchedIndex
+          // 假设如果有currentSong说明正在播放
+          this.isPlaying = true
+          LogUtil.info(`heanup 在歌单中找到当前播放的歌曲: ${this.songList[matchedIndex].name}, 索引: ${matchedIndex}`)
+        } else {
+          LogUtil.info(`heanup 当前播放的歌曲不在本歌单中`)
+        }
+      } else {
+        LogUtil.info(`heanup 当前没有播放歌曲`)
+      }
+    } catch (error) {
+      LogUtil.error('heanup 加载当前播放状态失败: ' + error)
+    }
+  }
+
   /**
    * 将 PlaylistSong 转换为 VideoItem
    */

+ 4 - 2
entry/src/main/ets/view/LocalMusic.ets

@@ -12028,14 +12028,16 @@ export struct LocalMusic {
     // 发送播放状态变化事件给歌单详情页面
     try {
       const eventPlaybackStatus: emitter.InnerEvent = { eventId: 2003 }
+      const currentFilePath = this.currentSong?.filePath || ''
       const eventData: emitter.EventData = {
         data: {
           isPlaying: this.isPlaying,
-          curIndex: this.curIndex
+          curIndex: this.curIndex,
+          currentFilePath: currentFilePath
         }
       };
       emitter.emit(eventPlaybackStatus, eventData)
-      Logger.info(`heanup 发送播放状态变化事件: isPlaying=${this.isPlaying}, curIndex=${this.curIndex}`)
+      Logger.info(`heanup 发送播放状态变化事件: isPlaying=${this.isPlaying}, curIndex=${this.curIndex}, currentFilePath=${currentFilePath}`)
     } catch (error) {
       Logger.error('heanup 发送播放状态变化事件失败: ' + error)
     }