|
|
@@ -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);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
|