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

+ 8 - 0
entry/src/main/ets/common/service/PlayerStateModel.ets

@@ -304,6 +304,14 @@ export class PlayerStateModel {
     LogUtils.getInstance().LOGI('PlayerStateModel: State listener removed');
   }
 
+  /**
+   * 手动触发状态变化通知
+   * 用于强制通知所有监听器当前状态
+   */
+  forceNotifyStateChanged(): void {
+    this.notifyStateChanged();
+  }
+
   /**
    * 通知状态变化
    */

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

@@ -2302,6 +2302,10 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
       // 确保播放列表状态是最新的
       this.updatePlaylistStateInModelSync();
       
+      // 触发状态监听器通知
+      this.stateModel.forceNotifyStateChanged();
+      
+      // 更新桌面卡片
       await this.updateAllForms(true); // 强制更新
       LogUtils.getInstance().LOGI('UnifiedPlayerService: Current state broadcasted manually');
     } catch (error) {

+ 131 - 1
entry/src/main/ets/view/LocalMusic.ets

@@ -37,7 +37,7 @@ import { effectKit } from '@kit.ArkGraphics2D';
 import { ColorConversion } from '../common/util/ColorConversion';
 import { LyricController, LyricParser, LyricView2 } from '@seagazer/cclyric';
 import { AvSessionController } from '../controller/AvSessionController';
-import { PlayerStateListener, PlayerState, PlayerError } from '../common/service/PlayerStateModel';
+import { PlayerStateListener, PlayerState, PlayerError, PlayerErrorType } from '../common/service/PlayerStateModel';
 
 import {
   IjkMediaPlayer,
@@ -479,6 +479,11 @@ export struct LocalMusic {
 
       LogUtils.getInstance().LOGI('LocalMusic: UnifiedPlayerService initialized successfully');
 
+      // 注册状态监听器,接收来自UnifiedPlayerService的状态变化通知
+      const stateListener = new LocalMusicStateListener(this);
+      this.unifiedPlayerService.addStateListener(stateListener);
+      LogUtils.getInstance().LOGI('LocalMusic: State listener registered with UnifiedPlayerService');
+
       // 初始化完成后,开始加载本地文件
       this.loadLocalFilesAfterServiceInit();
     } catch (error) {
@@ -541,6 +546,107 @@ export struct LocalMusic {
     });
   }
 
+  // ==================== PlayerStateListener 实现方法 ====================
+
+  /**
+   * 播放器状态变化监听器
+   * 当UnifiedPlayerService的状态发生变化时被调用
+   */
+  public onPlayerStateChanged(state: PlayerState): void {
+    try {
+      LogUtils.getInstance().LOGI(`LocalMusic: Received state change from UnifiedPlayerService - isPlaying: ${state.isPlaying}, isPaused: ${state.isPaused}, currentIndex: ${state.currentIndex}`);
+      
+      // 同步播放状态到UI
+      if (state.isPlaying !== undefined) {
+        // 根据播放状态更新UI播放按钮状态
+        if (state.isPlaying) {
+          this.CONTROL_PlayStatus = PlayStatus.PLAY;
+        } else if (state.isPaused) {
+          this.CONTROL_PlayStatus = PlayStatus.PAUSE;
+        } else {
+          this.CONTROL_PlayStatus = PlayStatus.INIT;
+        }
+      }
+
+      // 同步进度信息
+      if (state.currentPosition !== undefined && state.duration !== undefined) {
+        this.setProgress();
+      }
+
+      // 同步播放列表状态
+      if (state.currentIndex !== undefined && state.currentIndex !== this.curIndex) {
+        this.curIndex = state.currentIndex;
+      }
+
+      LogUtils.getInstance().LOGI('LocalMusic: UI state synchronized with UnifiedPlayerService');
+    } catch (error) {
+      LogUtils.getInstance().LOGI(`LocalMusic: Error handling state change: ${error}`);
+    }
+  }
+
+  /**
+   * 歌曲变化监听器
+   * 当UnifiedPlayerService的当前歌曲发生变化时被调用
+   */
+  public onPlayerSongChanged(song: VideoItem): void {
+    try {
+      LogUtils.getInstance().LOGI(`LocalMusic: Received song change from UnifiedPlayerService - ${song.name}`);
+      
+      // 同步当前歌曲信息到UI
+      this.currentSong = song;
+      this.videoUrl = song.filePath;
+      this.name = song.name;
+      this.artist = song.artist || this.UNKONWN;
+      this.cover = song.pixelMapPath;
+      
+      // 重置时间显示状态
+      this.oldSeconds = 0;
+      this.currentTime = "00:00";
+      this.totalTime = "00:00";
+      this.lastSongPath = song.filePath;
+      this.justSwitched = true;
+
+      // 触发封面动画
+      this.changeImageAnimation();
+
+      LogUtils.getInstance().LOGI('LocalMusic: UI song info synchronized with UnifiedPlayerService');
+    } catch (error) {
+      LogUtils.getInstance().LOGI(`LocalMusic: Error handling song change: ${error}`);
+    }
+  }
+
+  /**
+   * 播放器错误监听器
+   * 当UnifiedPlayerService发生错误时被调用
+   */
+  public onPlayerError(error: PlayerError): void {
+    try {
+      LogUtils.getInstance().LOGI(`LocalMusic: Received error from UnifiedPlayerService - ${error.type}: ${error.message}`);
+      
+      // 根据错误类型显示不同的提示
+      switch (error.type) {
+        case PlayerErrorType.FILE_NOT_FOUND:
+          ToastUtil.showToast('文件未找到,已跳过');
+          break;
+        case PlayerErrorType.NETWORK_ERROR:
+          ToastUtil.showToast('网络错误,请检查网络连接');
+          break;
+        case PlayerErrorType.PLAYBACK_ERROR:
+          ToastUtil.showToast('播放出错,正在重试');
+          break;
+        default:
+          ToastUtil.showToast(`播放器错误: ${error.message}`);
+          break;
+      }
+      
+      LogUtils.getInstance().LOGI('LocalMusic: Error handled and user notified');
+    } catch (error) {
+      LogUtils.getInstance().LOGI(`LocalMusic: Error handling player error: ${error}`);
+    }
+  }
+
+  // ==================== 组件生命周期 ====================
+
   // 组件生命周期
   aboutToAppear() {
     if(PreferencesUtil.getBooleanSync('isFirstTiped',true)){
@@ -12485,4 +12591,28 @@ function getFileDirName(filePath: string,rootPath:string): string{
   return result
 }
 
+/**
+ * LocalMusic 状态监听器实现类
+ * 用于接收 UnifiedPlayerService 的状态变化通知
+ */
+class LocalMusicStateListener implements PlayerStateListener {
+  private localMusic: LocalMusic;
+
+  constructor(localMusic: LocalMusic) {
+    this.localMusic = localMusic;
+  }
+
+  onStateChanged(state: PlayerState): void {
+    this.localMusic.onPlayerStateChanged(state);
+  }
+
+  onSongChanged(song: VideoItem): void {
+    this.localMusic.onPlayerSongChanged(song);
+  }
+
+  onError(error: PlayerError): void {
+    this.localMusic.onPlayerError(error);
+  }
+}
+