Przeglądaj źródła

修正播控集成播放、暂停状态未同步UI

chendeben 1 rok temu
rodzic
commit
f52e8389e3
1 zmienionych plików z 75 dodań i 32 usunięć
  1. 75 32
      entry/src/main/ets/view/LocalMusic.ets

+ 75 - 32
entry/src/main/ets/view/LocalMusic.ets

@@ -496,35 +496,45 @@ export struct LocalMusic {
         }
         
         onStateChanged(state: PlayerState): void {
-          // 获取播放器实际状态,而不是依赖状态模型
-          const actuallyPlaying = this.localMusic.unifiedPlayerService.getActualPlayingState();
+          LogUtils.getInstance().LOGI(`LocalMusic: StateListener triggered - isPlaying: ${state.isPlaying}, isPaused: ${state.isPaused}`);
+          
+          // 直接使用状态模型的状态,这是最权威的状态源
+          const isPlaying = state.isPlaying;
+          const isPaused = state.isPaused;
           
           // 同步播放状态到LocalMusic的UI状态
           const previousStatus: PlayStatus = this.localMusic.CONTROL_PlayStatus;
-          this.localMusic.CONTROL_PlayStatus = actuallyPlaying ? PlayStatus.PLAY : 
-                                   (state.isPaused ? PlayStatus.PAUSE : PlayStatus.INIT);
+          
+          // 根据状态模型确定UI状态
+          if (isPlaying) {
+            this.localMusic.CONTROL_PlayStatus = PlayStatus.PLAY;
+          } else if (isPaused) {
+            this.localMusic.CONTROL_PlayStatus = PlayStatus.PAUSE;
+          } else {
+            this.localMusic.CONTROL_PlayStatus = PlayStatus.INIT;
+          }
           
           // 更新播放状态相关的UI
-          this.localMusic.setIsPlaying(actuallyPlaying);
-          this.localMusic.updateSessionPlayState(actuallyPlaying);
+          this.localMusic.setIsPlaying(isPlaying);
+          this.localMusic.updateSessionPlayState(isPlaying);
           
-          LogUtils.getInstance().LOGI(`LocalMusic: State sync - StateModel: ${state.isPlaying}, ActualPlayer: ${actuallyPlaying}, UI: ${this.localMusic.CONTROL_PlayStatus}`);
+          LogUtils.getInstance().LOGI(`LocalMusic: State sync - Previous: ${previousStatus}, New: ${this.localMusic.CONTROL_PlayStatus}, isPlaying: ${isPlaying}`);
           
-          // 如果状态发生变化,触发UI更新
-          if (previousStatus !== this.localMusic.CONTROL_PlayStatus) {
-            this.localMusic.playChange();
-            this.localMusic.watchStatus();
-            
-            // 更新动画状态
-            if (state.isPlaying) {
-              this.localMusic.animationState = AnimationStatus.Running;
-              this.localMusic.mDestroyPage = false;
-            } else {
-              this.localMusic.animationState = AnimationStatus.Paused;
-              this.localMusic.mDestroyPage = true;
-            }
+          // 强制触发UI更新,无论状态是否变化
+          this.localMusic.playChange();
+          this.localMusic.watchStatus();
+          
+          // 更新动画状态
+          if (isPlaying) {
+            this.localMusic.animationState = AnimationStatus.Running;
+            this.localMusic.mDestroyPage = false;
+          } else {
+            this.localMusic.animationState = AnimationStatus.Paused;
+            this.localMusic.mDestroyPage = true;
           }
           
+          LogUtils.getInstance().LOGI(`LocalMusic: UI update completed - CONTROL_PlayStatus: ${this.localMusic.CONTROL_PlayStatus}, globalIsPlaying: ${this.localMusic.isPlaying}`);
+          
           // 同步播放模式
           if (this.localMusic.playType !== state.playMode) {
             this.localMusic.playType = state.playMode;
@@ -579,6 +589,10 @@ export struct LocalMusic {
       const stateListener = new LocalMusicStateListener(this);
       this.unifiedPlayerService.addStateListener(stateListener);
       
+      // 不设置LocalMusic自己的AVSession监听器,完全依赖UnifiedPlayerService
+      // UnifiedPlayerService已经设置了AVSession监听器,状态变化会通过StateListener传播到LocalMusic
+      LogUtils.getInstance().LOGI('LocalMusic: Relying on UnifiedPlayerService for AVSession handling');
+      
       // 初始化完成后,立即同步当前状态到UI
       setTimeout(() => {
         try {
@@ -7145,15 +7159,39 @@ export struct LocalMusic {
     // if(this.isCoverRectangle){
     //   return
     // }
-    if (this.CONTROL_PlayStatus === PlayStatus.PLAY) {
-      this.setIsPlaying(true)
-      this.animationState = AnimationStatus.Running
+    const isPlaying = this.CONTROL_PlayStatus === PlayStatus.PLAY;
+    
+    // 同步所有播放状态
+    this.setIsPlaying(isPlaying);
+    this.animationState = isPlaying ? AnimationStatus.Running : AnimationStatus.Paused;
+    
+    // 记录状态变化日志
+    LogUtils.getInstance().LOGI(`LocalMusic: playChange - CONTROL_PlayStatus: ${this.CONTROL_PlayStatus}, isPlaying: ${isPlaying}, globalIsPlaying: ${this.isPlaying}`);
+    
+    // 强制触发UI刷新 - 这对于@State变量应该是自动的,但我们确保一下
+    setTimeout(() => {
+      LogUtils.getInstance().LOGI(`LocalMusic: UI refresh triggered - CONTROL_PlayStatus: ${this.CONTROL_PlayStatus}`);
+    }, 50);
+  }
+
+  // 手动同步状态的方法,用于调试
+  public forceSyncState(): void {
+    const currentState = this.unifiedPlayerService.getCurrentState();
+    LogUtils.getInstance().LOGI(`LocalMusic: Force sync - UnifiedPlayerService state: isPlaying=${currentState.isPlaying}, isPaused=${currentState.isPaused}`);
+    
+    // 手动触发状态同步
+    if (currentState.isPlaying) {
+      this.CONTROL_PlayStatus = PlayStatus.PLAY;
+    } else if (currentState.isPaused) {
+      this.CONTROL_PlayStatus = PlayStatus.PAUSE;
     } else {
-      this.setIsPlaying(false)
-      this.animationState = AnimationStatus.Paused
+      this.CONTROL_PlayStatus = PlayStatus.INIT;
     }
-
-
+    
+    this.setIsPlaying(currentState.isPlaying);
+    this.playChange();
+    
+    LogUtils.getInstance().LOGI(`LocalMusic: Force sync completed - CONTROL_PlayStatus: ${this.CONTROL_PlayStatus}`);
   }
 
   // 定义停止旋转的方法
@@ -10444,10 +10482,18 @@ export struct LocalMusic {
   }
 
   private sessionPlayCallback = (): void => {
-    this.playOrPause()
+    LogUtils.getInstance().LOGI("LocalMusic: System play button pressed");
+    // 直接调用UnifiedPlayerService的方法,而不是LocalMusic的playOrPause
+    this.unifiedPlayerService.startPlayOrResumePlay().catch((error: Error) => {
+      LogUtils.getInstance().LOGI(`LocalMusic: System play command failed: ${error}`);
+    });
   };
   private sessionPauseCallback = (): void => {
-    this.playOrPause()
+    LogUtils.getInstance().LOGI("LocalMusic: System pause button pressed");
+    // 直接调用UnifiedPlayerService的方法,而不是LocalMusic的playOrPause
+    this.unifiedPlayerService.pause().catch((error: Error) => {
+      LogUtils.getInstance().LOGI(`LocalMusic: System pause command failed: ${error}`);
+    });
   };
 
   private playOrPause() {
@@ -10457,12 +10503,9 @@ export struct LocalMusic {
 
     if (this.CONTROL_PlayStatus === PlayStatus.PLAY) {
       this.pause();
-
-
     } else {
       this.startPlayOrResumePlay();
       this.playChange()
-
     }
   }