|
|
@@ -445,12 +445,40 @@ export struct LocalMusic {
|
|
|
}
|
|
|
|
|
|
// 初始化统一播放器服务
|
|
|
- private async initUnifiedPlayerService() {
|
|
|
+ private async initUnifiedPlayerService(): Promise<void> {
|
|
|
try {
|
|
|
await this.unifiedPlayerService.initialize(this.context);
|
|
|
|
|
|
- // 设置播放列表(如果已有)
|
|
|
- if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
+ // 从UnifiedPlayerService恢复播放列表和状态
|
|
|
+ const restoredPlaylist = this.unifiedPlayerService.getPlaylist();
|
|
|
+ const restoredIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
|
+ const restoredSong = this.unifiedPlayerService.getCurrentSong();
|
|
|
+
|
|
|
+ if (ArrayUtil.isNotEmpty(restoredPlaylist)) {
|
|
|
+ // 恢复播放列表到LocalMusic
|
|
|
+ this.songList = restoredPlaylist;
|
|
|
+ this.curIndex = restoredIndex;
|
|
|
+ this.currentSong = restoredSong || undefined;
|
|
|
+
|
|
|
+ // 同步到AppStorage
|
|
|
+ AppStorage.setOrCreate('songList', this.songList);
|
|
|
+ AppStorage.setOrCreate('currIndex', this.curIndex);
|
|
|
+ AppStorage.setOrCreate('currentSong', this.currentSong);
|
|
|
+
|
|
|
+ // 更新UI数据源
|
|
|
+ this.sonDataSource.pushArrayData(this.songList);
|
|
|
+
|
|
|
+ // 如果有当前歌曲,更新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: Restored playlist from UnifiedPlayerService - ${this.songList.length} songs, index ${this.curIndex}`);
|
|
|
+ } else if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
+ // 如果LocalMusic有播放列表但UnifiedPlayerService没有,设置到服务中
|
|
|
this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
|
}
|
|
|
|
|
|
@@ -547,12 +575,81 @@ export struct LocalMusic {
|
|
|
this.unifiedPlayerService.addStateListener(stateListener);
|
|
|
|
|
|
LogUtils.getInstance().LOGI('LocalMusic: UnifiedPlayerService initialized successfully');
|
|
|
+
|
|
|
+ // 初始化完成后,开始加载本地文件
|
|
|
+ this.loadLocalFilesAfterServiceInit();
|
|
|
} catch (error) {
|
|
|
LogUtils.getInstance().LOGI(`LocalMusic: Failed to initialize UnifiedPlayerService: ${error}`);
|
|
|
ToastUtil.showToast('播放器服务初始化失败');
|
|
|
+
|
|
|
+ // 即使服务初始化失败,也要加载本地文件
|
|
|
+ this.loadLocalFilesAfterServiceInit();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private loadLocalFilesAfterServiceInit() {
|
|
|
+ // 加载本地文件,然后恢复播放列表
|
|
|
+ this.getSortedFiles(this.rootPath).then(async () => {
|
|
|
+ this.isFavMusic = false;
|
|
|
+
|
|
|
+ // 等待UnifiedPlayerService完成数据恢复
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: Waiting for UnifiedPlayerService data restoration...');
|
|
|
+ const dataRestored: boolean = await this.unifiedPlayerService.waitForDataRestoration(3000);
|
|
|
+
|
|
|
+ if (dataRestored) {
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: UnifiedPlayerService data restoration completed');
|
|
|
+ } else {
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: UnifiedPlayerService data restoration timeout, proceeding anyway');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试从UnifiedPlayerService恢复播放列表
|
|
|
+ const unifiedPlaylist = this.unifiedPlayerService.getPlaylist();
|
|
|
+ const unifiedIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
|
+ const unifiedSong = this.unifiedPlayerService.getCurrentSong();
|
|
|
+
|
|
|
+ if (ArrayUtil.isNotEmpty(unifiedPlaylist)) {
|
|
|
+ // 使用UnifiedPlayerService的数据
|
|
|
+ this.songList = unifiedPlaylist;
|
|
|
+ this.curIndex = unifiedIndex;
|
|
|
+ this.currentSong = unifiedSong || undefined;
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Using playlist from UnifiedPlayerService - ${this.songList.length} songs`);
|
|
|
+ } else {
|
|
|
+ // 回退到旧的存储方式
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: UnifiedPlayerService playlist empty, falling back to legacy storage');
|
|
|
+ this.songList = PreferencesUtil.getSync('LastMusicList', []) as Array<VideoItem>
|
|
|
+ this.currentSong = PreferencesUtil.getSync('LastMusicInfo', undefined) as VideoItem
|
|
|
+ if (ArrayUtil.isEmpty(this.songList)) {
|
|
|
+ this.songList = this.getCurFileList()
|
|
|
+ } else {
|
|
|
+ this.curIndex = Utility.getIndexFromList(this.songList, this.currentSong.filePath)
|
|
|
+ }
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Using legacy playlist storage - ${this.songList.length} songs`);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 同步播放列表和当前索引到AppStorage,确保卡片能访问
|
|
|
+ AppStorage.setOrCreate('songList', this.songList);
|
|
|
+ AppStorage.setOrCreate('currIndex', this.curIndex);
|
|
|
+
|
|
|
+ if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
+ this.isFirstStartPlay = true
|
|
|
+ this.sonDataSource.pushArrayData(this.songList)
|
|
|
+ if (this.currentSong === undefined) {
|
|
|
+ this.isFirstStartPlay = false
|
|
|
+ this.currentSong = this.songList[0]
|
|
|
+ }
|
|
|
+ this.videoUrl = this.currentSong.filePath
|
|
|
+ this.name = this.currentSong.name
|
|
|
+ this.cover = this.currentSong.pixelMapPath
|
|
|
+ this.artist = this.currentSong.artist
|
|
|
+
|
|
|
+ // 同步当前歌曲到AppStorage
|
|
|
+ AppStorage.setOrCreate('currentSong', this.currentSong);
|
|
|
+
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Playlist restored - ${this.songList.length} songs, current: ${this.currentSong.name}`);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
// 组件生命周期
|
|
|
aboutToAppear() {
|
|
|
if(PreferencesUtil.getBooleanSync('isFirstTiped',true)){
|
|
|
@@ -568,10 +665,12 @@ export struct LocalMusic {
|
|
|
this.packName = AppUtil.getBundleName()
|
|
|
this.loadCacheFromStorage(); // 加载缓存
|
|
|
|
|
|
- // 初始化统一播放器服务
|
|
|
- this.initUnifiedPlayerService();
|
|
|
+ // 初始化统一播放器服务,然后加载文件
|
|
|
+ this.initUnifiedPlayerService().then(() => {
|
|
|
+ this.mkDownLoadDir();
|
|
|
+ });
|
|
|
|
|
|
- this.mkDownLoadDir()
|
|
|
+ // 注意:getSortedFiles的调用已经移到initUnifiedPlayerService完成后
|
|
|
|
|
|
let eventMusic: emitter.InnerEvent = { eventId: 2 }
|
|
|
// 监听广播事件(打开其他应用处理)
|
|
|
@@ -938,12 +1037,27 @@ export struct LocalMusic {
|
|
|
//穿山甲
|
|
|
// this.loadBannerAd(CSJUtil.getBannerID())
|
|
|
|
|
|
- this.songList = PreferencesUtil.getSync('LastMusicList', []) as Array<VideoItem>
|
|
|
- this.currentSong = PreferencesUtil.getSync('LastMusicInfo', undefined) as VideoItem
|
|
|
- if (ArrayUtil.isEmpty(this.songList)) {
|
|
|
- this.songList = this.getCurFileList()
|
|
|
+ // 优先从UnifiedPlayerService恢复播放列表,如果没有则从旧的存储恢复
|
|
|
+ const unifiedPlaylist = this.unifiedPlayerService.getPlaylist();
|
|
|
+ const unifiedIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
|
+ const unifiedSong = this.unifiedPlayerService.getCurrentSong();
|
|
|
+
|
|
|
+ if (ArrayUtil.isNotEmpty(unifiedPlaylist)) {
|
|
|
+ // 使用UnifiedPlayerService的数据
|
|
|
+ this.songList = unifiedPlaylist;
|
|
|
+ this.curIndex = unifiedIndex;
|
|
|
+ this.currentSong = unifiedSong || undefined;
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Using playlist from UnifiedPlayerService - ${this.songList.length} songs`);
|
|
|
} else {
|
|
|
- this.curIndex = Utility.getIndexFromList(this.songList, this.currentSong.filePath)
|
|
|
+ // 回退到旧的存储方式
|
|
|
+ this.songList = PreferencesUtil.getSync('LastMusicList', []) as Array<VideoItem>
|
|
|
+ this.currentSong = PreferencesUtil.getSync('LastMusicInfo', undefined) as VideoItem
|
|
|
+ if (ArrayUtil.isEmpty(this.songList)) {
|
|
|
+ this.songList = this.getCurFileList()
|
|
|
+ } else {
|
|
|
+ this.curIndex = Utility.getIndexFromList(this.songList, this.currentSong.filePath)
|
|
|
+ }
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Using legacy playlist storage - ${this.songList.length} songs`);
|
|
|
}
|
|
|
|
|
|
// 同步播放列表和当前索引到AppStorage,确保卡片能访问
|
|
|
@@ -1252,6 +1366,22 @@ export struct LocalMusic {
|
|
|
}
|
|
|
}
|
|
|
this.dataSource.pushArrayData(this.videoLocalList)
|
|
|
+
|
|
|
+ // 如果当前有播放列表,更新播放列表到统一播放器服务
|
|
|
+ if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
+ const globalVideoList = Utility.getGlobalList(this.videoLocalList, CommonConstants.TYPE_LOCAL) as VideoItem[];
|
|
|
+ if (ArrayUtil.isNotEmpty(globalVideoList)) {
|
|
|
+ this.songList = globalVideoList;
|
|
|
+ AppStorage.setOrCreate('songList', this.songList);
|
|
|
+ this.sonDataSource.pushArrayData(this.songList);
|
|
|
+
|
|
|
+ // 同步到统一播放器服务进行持久化
|
|
|
+ this.syncPlaylistToService();
|
|
|
+
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: updateListData - Updated playlist with ${this.songList.length} songs`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
animateTo({ duration: 888 }, () => {
|
|
|
this.opacityItem = 1;
|
|
|
});
|