فهرست منبع

修复:界面显示与实际播放状态不一致

chendeben 1 سال پیش
والد
کامیت
39bf7be299

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

@@ -62,6 +62,7 @@ export interface IPlayerService {
   
   // 状态查询 - 兼容LocalMusic数据结构
   getCurrentState(): PlayerState;
+  getActualPlayingState(): boolean;
   getCurrentSong(): VideoItem | null;
   getPlaylist(): VideoItem[];
   getCurrentIndex(): number;
@@ -119,6 +120,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
   private progressTimer: number = -1;
   private isInitialized: boolean = false;
   private isDataRestored: boolean = false; // 新增:数据恢复完成标识
+
   private isPlayerPreparedForCurrentSong: boolean = false; // 新增:播放器是否已为当前歌曲准备就绪
   private currentRetryCount: number = 0;
   private lastAvSessionUpdate: number = 0; 
@@ -682,6 +684,23 @@ async initialize(context: common.UIAbilityContext): Promise<void> {
     return this.stateModel.getState();
   }
 
+  /**
+   * 获取播放器实际播放状态(最准确)
+   * 直接从ijkPlayer获取,不依赖状态模型
+   */
+  getActualPlayingState(): boolean {
+    try {
+      const ijkPlayer = this.playerManager?.getIjkPlayer();
+      if (ijkPlayer) {
+        return ijkPlayer.isPlaying();
+      }
+      return false;
+    } catch (error) {
+      LogUtils.getInstance().LOGI(`UnifiedPlayerService: Failed to get actual playing state: ${error}`);
+      return false;
+    }
+  }
+
   getCurrentSong(): VideoItem | null {
     return this.playlistModel.getCurrentSong();
   }
@@ -1655,7 +1674,7 @@ async initialize(context: common.UIAbilityContext): Promise<void> {
         this.stateModel.updateSpeed(stateData.speed);
         this.stateModel.updatePlayMode(stateData.playMode);
         
-        // 恢复播放状态
+        // 恢复播放状态(UI会使用实际播放器状态,所以这里可以恢复逻辑状态)
         this.stateModel.updatePlayingState(stateData.isPlaying || false);
         
         // 恢复当前播放索引

+ 13 - 0
entry/src/main/ets/entryability/EntryAbility.ets

@@ -615,6 +615,19 @@ export default class EntryAbility extends UIAbility {
                     break;
             }
             
+            // 操作完成后,广播最新状态给主应用UI和桌面卡片
+            setTimeout(() => {
+                this.broadcastCurrentPlayerState();
+                hilog.info(0x0000, 'Heanup2', '📡 桌面卡片操作后广播状态更新');
+                
+                // 同时触发UnifiedPlayerService的状态广播,确保所有监听器都能收到更新
+                try {
+                    unifiedService.broadcastCurrentState();
+                } catch (error) {
+                    hilog.error(0x0000, 'Heanup2', `❌ 触发服务状态广播失败: ${error}`);
+                }
+            }, 100); // 短延迟,确保操作完全完成
+            
             hilog.info(0x0000, 'Heanup2', `✅ Widget control command processed: ${command}`);
             
         } catch (error) {

+ 22 - 3
entry/src/main/ets/view/LocalMusic.ets

@@ -496,14 +496,19 @@ export struct LocalMusic {
         }
         
         onStateChanged(state: PlayerState): void {
+          // 获取播放器实际状态,而不是依赖状态模型
+          const actuallyPlaying = this.localMusic.unifiedPlayerService.getActualPlayingState();
+          
           // 同步播放状态到LocalMusic的UI状态
           const previousStatus: PlayStatus = this.localMusic.CONTROL_PlayStatus;
-          this.localMusic.CONTROL_PlayStatus = state.isPlaying ? PlayStatus.PLAY : 
+          this.localMusic.CONTROL_PlayStatus = actuallyPlaying ? PlayStatus.PLAY : 
                                    (state.isPaused ? PlayStatus.PAUSE : PlayStatus.INIT);
           
           // 更新播放状态相关的UI
-          this.localMusic.setIsPlaying(state.isPlaying);
-          this.localMusic.updateSessionPlayState(state.isPlaying);
+          this.localMusic.setIsPlaying(actuallyPlaying);
+          this.localMusic.updateSessionPlayState(actuallyPlaying);
+          
+          LogUtils.getInstance().LOGI(`LocalMusic: State sync - StateModel: ${state.isPlaying}, ActualPlayer: ${actuallyPlaying}, UI: ${this.localMusic.CONTROL_PlayStatus}`);
           
           // 如果状态发生变化,触发UI更新
           if (previousStatus !== this.localMusic.CONTROL_PlayStatus) {
@@ -574,6 +579,20 @@ export struct LocalMusic {
       const stateListener = new LocalMusicStateListener(this);
       this.unifiedPlayerService.addStateListener(stateListener);
       
+      // 初始化完成后,立即同步当前状态到UI
+      setTimeout(() => {
+        try {
+          const currentState = this.unifiedPlayerService.getCurrentState();
+          const actuallyPlaying = this.unifiedPlayerService.getActualPlayingState();
+          LogUtils.getInstance().LOGI(`LocalMusic: Syncing current state - StateModel: ${currentState.isPlaying}, ActualPlayer: ${actuallyPlaying}`);
+          
+          // 手动触发状态同步,确保UI显示正确(使用实际播放器状态)
+          stateListener.onStateChanged(currentState);
+        } catch (error) {
+          LogUtils.getInstance().LOGI(`LocalMusic: Failed to sync current state: ${error}`);
+        }
+      }, 200); // 短延迟,确保组件完全初始化
+      
       LogUtils.getInstance().LOGI('LocalMusic: UnifiedPlayerService initialized successfully');
       
       // 初始化完成后,开始加载本地文件