Răsfoiți Sursa

修复播放器准备状态和UI同步问题,确保在播放状态下正确更新进度和当前歌曲信息

chendeben 11 luni în urmă
părinte
comite
0e04137050

+ 12 - 0
entry/src/main/ets/common/service/UnifiedPlayerService.ets

@@ -836,6 +836,11 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
     this.stateModel.updateLoadingState(false);
     this.stateModel.updatePlayingState(true);
     this.playerManager.clearPausedState();
+    
+    // 关键修复:确保播放器准备状态标记被正确设置
+    this.isPlayerPreparedForCurrentSong = true;
+    console.log("Heanup2 UnifiedPlayerService: 设置播放器准备状态为true,确保进度更新正常");
+    
     this.startProgressTimer();
     this.updateSessionPlayState();
     this.updateWidgetsForStateChange();
@@ -2747,6 +2752,13 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
           // 卡片显示的是播放按钮(UI为暂停/停止状态),所以执行播放
           LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService: Widget state is PAUSED, will play.');
           await this.startPlayOrResumePlay();
+          
+          // 关键修复:播放开始后,确保播放器准备状态正确设置
+          const ijkPlayer = this.playerManager.getIjkPlayer();
+          if (ijkPlayer && ijkPlayer.isPlaying()) {
+            this.isPlayerPreparedForCurrentSong = true;
+            LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService: Widget play success - set player prepared state to true');
+          }
         }
       } else {
         // 向后兼容的旧逻辑:根据播放器实际状态切换

+ 104 - 37
entry/src/main/ets/view/LocalMusic.ets

@@ -484,6 +484,21 @@ export struct LocalMusic {
           const currentState = this.unifiedPlayerService.getCurrentState();
           const actuallyPlaying = this.unifiedPlayerService.getActualPlayingState();
           LogUtils.getInstance().LOGI(`Heanup LocalMusic: Syncing current state - StateModel: ${currentState.isPlaying}, ActualPlayer: ${actuallyPlaying}`);
+          
+          // 关键修复:如果播放器正在播放,立即同步UI状态和进度
+          if (actuallyPlaying) {
+            LogUtils.getInstance().LOGI('LocalMusic: Player is playing during init, syncing UI state');
+            this.setIsPlaying(true);
+            this.CONTROL_PlayStatus = PlayStatus.PLAY;
+            this.mDestroyPage = false;
+            
+            // 立即启动进度定时器并更新一次进度
+            this.stopProgressTask();
+            this.startProgressTask();
+            this.setProgress(); // 立即更新一次UI
+            
+            this.playChange(); // 更新播放状态UI
+          }
         } catch (error) {
           LogUtils.getInstance().LOGI(`Heanup LocalMusic: Failed to sync current state: ${error}`);
         }
@@ -525,6 +540,17 @@ export struct LocalMusic {
         this.songList = unifiedPlaylist;
         this.curIndex = unifiedIndex;
         this.currentSong = unifiedSong || undefined;
+        
+        // 关键修复:如果有当前歌曲,立即更新UI显示信息
+        if (this.currentSong) {
+          this.videoUrl = this.currentSong.filePath;
+          this.name = this.currentSong.name;
+          this.artist = this.currentSong.artist;
+          this.cover = this.currentSong.pixelMapPath;
+          
+          LogUtils.getInstance().LOGI(`LocalMusic: Updated UI with current song: ${this.currentSong.name}, path: ${this.currentSong.filePath}`);
+        }
+        
         LogUtils.getInstance().LOGI(`Heanup LocalMusic: Using playlist from UnifiedPlayerService - ${this.songList.length} songs, index ${this.curIndex}, current song: ${this.currentSong?.name || 'null'}`);
       } else {
         // 回退到旧的存储方式
@@ -966,6 +992,7 @@ export struct LocalMusic {
   /**
    * 页面显示时同步播放状态,修复音频中断后的状态不一致问题
    * 优化:使用异步执行,避免阻塞页面显示
+   * 修复:确保进度定时器在播放状态下正确启动
    */
   private syncPlaybackStateOnShow(): void {
     // 异步执行状态同步,避免阻塞页面显示
@@ -984,6 +1011,31 @@ export struct LocalMusic {
           this.playChange();
           this.watchStatus(); // 现在已优化为异步执行
         }
+
+        // 关键修复:确保当前歌曲信息与播放器状态同步
+        const currentSong = this.unifiedPlayerService.getCurrentSong();
+        if (currentSong && currentSong !== this.currentSong) {
+          LogUtils.getInstance().LOGI(`onPageShow: Syncing current song info from service: ${currentSong.name}`);
+          this.currentSong = currentSong;
+          this.videoUrl = currentSong.filePath;
+          this.name = currentSong.name;
+          this.artist = currentSong.artist;
+          this.cover = currentSong.pixelMapPath;
+        }
+
+        // 关键修复:确保在播放状态下启动进度定时器
+        if (actualPlaying) {
+          LogUtils.getInstance().LOGI(`onPageShow: Player is playing, ensuring progress timer is running`);
+          this.mDestroyPage = false; // 确保进度更新不被阻止
+          this.stopProgressTask(); // 先停止现有定时器
+          this.startProgressTask(); // 重新启动定时器
+          
+          // 立即更新一次进度,确保UI显示最新状态
+          this.setProgress();
+        } else {
+          LogUtils.getInstance().LOGI(`onPageShow: Player not playing, stopping progress timer`);
+          this.stopProgressTask();
+        }
       } catch (error) {
         LogUtils.getInstance().LOGI(`syncPlaybackStateOnShow error: ${error}`);
       }
@@ -11268,51 +11320,66 @@ export struct LocalMusic {
 
 
   private setProgress() {
-    let ijkPlayer=this.unifiedPlayerService.getIjkPlayer() as IjkMediaPlayer;
-    let position = ijkPlayer.getCurrentPosition();
-    let duration = ijkPlayer.getDuration();
-    let pos = 0;
-    if (duration > 0) {
-      this.slideEnable = true;
-      let curPercent = position / duration;
-      pos = curPercent * 100;
-      if (pos > this.PROGRESS_MAX_VALUE) {
-        this.progressValue = this.PROGRESS_MAX_VALUE
+    try {
+      let ijkPlayer = this.unifiedPlayerService.getIjkPlayer() as IjkMediaPlayer;
+      
+      // 关键修复:检查播放器是否存在
+      if (!ijkPlayer) {
+        LogUtils.getInstance().LOGI("setProgress: IJK player not available");
+        return;
+      }
+      
+      let position = ijkPlayer.getCurrentPosition();
+      let duration = ijkPlayer.getDuration();
+      let pos = 0;
+      
+      // 关键修复:添加详细的调试日志
+      LogUtils.getInstance().LOGI(`setProgress: position=${position}ms, duration=${duration}ms`);
+      
+      if (duration > 0) {
+        this.slideEnable = true;
+        let curPercent = position / duration;
+        pos = curPercent * 100;
+        if (pos > this.PROGRESS_MAX_VALUE) {
+          this.progressValue = this.PROGRESS_MAX_VALUE
+        } else {
+          this.progressValue = pos;
+        }
       } else {
-        this.progressValue = pos;
+        // 关键修复:duration为0时的处理
+        LogUtils.getInstance().LOGI("setProgress: Duration is 0, disabling slider");
+        this.slideEnable = false;
+        this.progressValue = 0;
       }
-    }
-
 
-    // LogUtils.getInstance()
-    //   .LOGI("setProgress position:" + position + ",duration:" + duration + ",progressValue:" + pos);
-    this.totalTime = this.stringForTime(duration);
-    if (position > duration) {
-      position = duration;
-    }
-    this.isCurrentTime = true;
-    this.lyricController.updatePosition(position + this.timeOffset * 1000)
-    this.lyricControllerXF.updatePosition(position + this.timeOffset * 1000)
-    this.lyricControllerSingle.updatePosition(position + this.timeOffset * 1000)
-
-    this.currentTime = this.stringForTime(position);
-    this.isCurrentTime = false
+      this.totalTime = this.stringForTime(duration);
+      if (position > duration) {
+        position = duration;
+      }
+      this.isCurrentTime = true;
+      this.lyricController.updatePosition(position + this.timeOffset * 1000)
+      this.lyricControllerXF.updatePosition(position + this.timeOffset * 1000)
+      this.lyricControllerSingle.updatePosition(position + this.timeOffset * 1000)
 
+      this.currentTime = this.stringForTime(position);
+      this.isCurrentTime = false
 
-    if (ijkPlayer.isPlaying()) {
-      this.randomColor =
-        `rgb(${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)})`
+      if (ijkPlayer.isPlaying()) {
+        this.randomColor =
+          `rgb(${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)})`
 
-      // 判断是否播放到结束时间
-      if (this.isOpenJump && duration > this.jumpEndTime * 1000) {
-        if (position >= duration - this.jumpEndTime * 1000) {
-          this.playNext()
+        // 判断是否播放到结束时间
+        if (this.isOpenJump && duration > this.jumpEndTime * 1000) {
+          if (position >= duration - this.jumpEndTime * 1000) {
+            this.playNext()
+          }
         }
-      }
-
-    } else {
-      this.randomColor = 'rbg(0,0,0)'
 
+      } else {
+        this.randomColor = 'rbg(0,0,0)'
+      }
+    } catch (error) {
+      LogUtils.getInstance().LOGI(`setProgress error: ${error}`);
     }
     // try {
     //   // 优先从UnifiedPlayerService获取进度信息