|
@@ -440,6 +440,16 @@ export struct LocalMusic {
|
|
|
try {
|
|
try {
|
|
|
await this.unifiedPlayerService.initialize(this.context);
|
|
await this.unifiedPlayerService.initialize(this.context);
|
|
|
|
|
|
|
|
|
|
+ // 等待数据恢复完成后再获取播放列表
|
|
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: Waiting for UnifiedPlayerService data restoration...');
|
|
|
|
|
+ const dataRestored: boolean = await this.unifiedPlayerService.waitForDataRestoration(5000);
|
|
|
|
|
+
|
|
|
|
|
+ if (!dataRestored) {
|
|
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: Data restoration timeout, but proceeding with initialization');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: Data restoration completed successfully');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 从UnifiedPlayerService恢复播放列表和状态
|
|
// 从UnifiedPlayerService恢复播放列表和状态
|
|
|
const restoredPlaylist = this.unifiedPlayerService.getPlaylist();
|
|
const restoredPlaylist = this.unifiedPlayerService.getPlaylist();
|
|
|
const restoredIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
const restoredIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
@@ -467,10 +477,14 @@ export struct LocalMusic {
|
|
|
this.cover = this.currentSong.pixelMapPath;
|
|
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);
|
|
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Restored playlist from UnifiedPlayerService - ${this.songList.length} songs, index ${this.curIndex}, current song: ${this.currentSong?.name || 'null'}`);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: No playlist restored from UnifiedPlayerService, songList length = ' + this.songList.length);
|
|
|
|
|
+ if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
|
|
+ // 如果LocalMusic有播放列表但UnifiedPlayerService没有,设置到服务中
|
|
|
|
|
+ this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Set existing playlist to UnifiedPlayerService - ${this.songList.length} songs`);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 添加状态监听器,保持UI同步
|
|
// 添加状态监听器,保持UI同步
|
|
@@ -578,15 +592,8 @@ export struct LocalMusic {
|
|
|
this.getSortedFiles(this.rootPath).then(async () => {
|
|
this.getSortedFiles(this.rootPath).then(async () => {
|
|
|
this.isFavMusic = false;
|
|
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');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 数据恢复已在initUnifiedPlayerService中完成,直接获取播放列表
|
|
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: Getting playlist from UnifiedPlayerService after file loading');
|
|
|
|
|
|
|
|
// 尝试从UnifiedPlayerService恢复播放列表
|
|
// 尝试从UnifiedPlayerService恢复播放列表
|
|
|
const unifiedPlaylist = this.unifiedPlayerService.getPlaylist();
|
|
const unifiedPlaylist = this.unifiedPlayerService.getPlaylist();
|
|
@@ -598,18 +605,22 @@ export struct LocalMusic {
|
|
|
this.songList = unifiedPlaylist;
|
|
this.songList = unifiedPlaylist;
|
|
|
this.curIndex = unifiedIndex;
|
|
this.curIndex = unifiedIndex;
|
|
|
this.currentSong = unifiedSong || undefined;
|
|
this.currentSong = unifiedSong || undefined;
|
|
|
- LogUtils.getInstance().LOGI(`LocalMusic: Using playlist from UnifiedPlayerService - ${this.songList.length} songs`);
|
|
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Using playlist from UnifiedPlayerService - ${this.songList.length} songs, index ${this.curIndex}, current song: ${this.currentSong?.name || 'null'}`);
|
|
|
} else {
|
|
} else {
|
|
|
// 回退到旧的存储方式
|
|
// 回退到旧的存储方式
|
|
|
LogUtils.getInstance().LOGI('LocalMusic: UnifiedPlayerService playlist empty, falling back to legacy storage');
|
|
LogUtils.getInstance().LOGI('LocalMusic: UnifiedPlayerService playlist empty, falling back to legacy storage');
|
|
|
this.songList = PreferencesUtil.getSync('LastMusicList', []) as Array<VideoItem>
|
|
this.songList = PreferencesUtil.getSync('LastMusicList', []) as Array<VideoItem>
|
|
|
this.currentSong = PreferencesUtil.getSync('LastMusicInfo', undefined) as VideoItem
|
|
this.currentSong = PreferencesUtil.getSync('LastMusicInfo', undefined) as VideoItem
|
|
|
if (ArrayUtil.isEmpty(this.songList)) {
|
|
if (ArrayUtil.isEmpty(this.songList)) {
|
|
|
- this.songList = this.getCurFileList()
|
|
|
|
|
|
|
+ const currentFileList = this.getCurFileList();
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: No legacy playlist found, current directory has ${currentFileList.length} songs`);
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Current path: ${this.currentPath}, Root path: ${this.rootPath}`);
|
|
|
|
|
+ this.songList = currentFileList;
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Using current file list as fallback - ${this.songList.length} songs`);
|
|
|
} else {
|
|
} else {
|
|
|
this.curIndex = Utility.getIndexFromList(this.songList, this.currentSong.filePath)
|
|
this.curIndex = Utility.getIndexFromList(this.songList, this.currentSong.filePath)
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Using legacy playlist storage - ${this.songList.length} songs, index ${this.curIndex}`);
|
|
|
}
|
|
}
|
|
|
- LogUtils.getInstance().LOGI(`LocalMusic: Using legacy playlist storage - ${this.songList.length} songs`);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 同步播放列表和当前索引到AppStorage,确保卡片能访问
|
|
// 同步播放列表和当前索引到AppStorage,确保卡片能访问
|
|
@@ -1008,48 +1019,28 @@ export struct LocalMusic {
|
|
|
//穿山甲
|
|
//穿山甲
|
|
|
// this.loadBannerAd(CSJUtil.getBannerID())
|
|
// this.loadBannerAd(CSJUtil.getBannerID())
|
|
|
|
|
|
|
|
- // 优先从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.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);
|
|
|
|
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: mkDownLoadDir - Files loaded, skipping playlist restoration (already done in initUnifiedPlayerService)');
|
|
|
|
|
|
|
|
|
|
+ // 播放列表恢复逻辑已经在 initUnifiedPlayerService 中完成,这里不需要重复
|
|
|
|
|
+ // 只需要确保UI状态是最新的
|
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
this.isFirstStartPlay = true
|
|
this.isFirstStartPlay = true
|
|
|
this.sonDataSource.pushArrayData(this.songList)
|
|
this.sonDataSource.pushArrayData(this.songList)
|
|
|
- if (this.currentSong === undefined) {
|
|
|
|
|
|
|
+ if (this.currentSong === undefined && this.songList.length > 0) {
|
|
|
this.isFirstStartPlay = false
|
|
this.isFirstStartPlay = false
|
|
|
this.currentSong = this.songList[0]
|
|
this.currentSong = this.songList[0]
|
|
|
}
|
|
}
|
|
|
- this.videoUrl = this.currentSong.filePath
|
|
|
|
|
- this.name = this.currentSong.name
|
|
|
|
|
- this.cover = this.currentSong.pixelMapPath
|
|
|
|
|
- AppStorage.setOrCreate('currentSong',this.currentSong) ;
|
|
|
|
|
|
|
+ if (this.currentSong) {
|
|
|
|
|
+ this.videoUrl = this.currentSong.filePath
|
|
|
|
|
+ this.name = this.currentSong.name
|
|
|
|
|
+ this.cover = this.currentSong.pixelMapPath
|
|
|
|
|
+ AppStorage.setOrCreate('currentSong',this.currentSong);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: mkDownLoadDir - UI updated with existing playlist: ${this.songList.length} songs`);
|
|
|
} else {
|
|
} else {
|
|
|
this.name = '空空如也'
|
|
this.name = '空空如也'
|
|
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: mkDownLoadDir - No playlist available, showing empty state');
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -1332,10 +1323,13 @@ export struct LocalMusic {
|
|
|
}
|
|
}
|
|
|
this.dataSource.pushArrayData(this.videoLocalList)
|
|
this.dataSource.pushArrayData(this.videoLocalList)
|
|
|
|
|
|
|
|
- // 如果当前有播放列表,更新播放列表到统一播放器服务
|
|
|
|
|
|
|
+ // 如果当前有播放列表,检查是否需要更新播放列表到统一播放器服务
|
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
const globalVideoList = Utility.getGlobalList(this.videoLocalList, CommonConstants.TYPE_LOCAL) as VideoItem[];
|
|
const globalVideoList = Utility.getGlobalList(this.videoLocalList, CommonConstants.TYPE_LOCAL) as VideoItem[];
|
|
|
- if (ArrayUtil.isNotEmpty(globalVideoList)) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 只有在没有现有播放列表时,才使用当前目录的文件列表
|
|
|
|
|
+ // 避免在文件列表更新时破坏用户的播放列表
|
|
|
|
|
+ if (ArrayUtil.isEmpty(this.songList) && ArrayUtil.isNotEmpty(globalVideoList)) {
|
|
|
this.songList = globalVideoList;
|
|
this.songList = globalVideoList;
|
|
|
AppStorage.setOrCreate('songList', this.songList);
|
|
AppStorage.setOrCreate('songList', this.songList);
|
|
|
this.sonDataSource.pushArrayData(this.songList);
|
|
this.sonDataSource.pushArrayData(this.songList);
|
|
@@ -1343,7 +1337,9 @@ export struct LocalMusic {
|
|
|
// 同步到统一播放器服务进行持久化
|
|
// 同步到统一播放器服务进行持久化
|
|
|
this.syncPlaylistToService();
|
|
this.syncPlaylistToService();
|
|
|
|
|
|
|
|
- LogUtils.getInstance().LOGI(`LocalMusic: updateListData - Updated playlist with ${this.songList.length} songs`);
|
|
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: updateListData - Created initial playlist with ${this.songList.length} songs`);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: updateListData - Preserving existing playlist with ${this.songList.length} songs, current directory has ${globalVideoList.length} songs`);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -4745,13 +4741,27 @@ export struct LocalMusic {
|
|
|
if (index !== undefined) {
|
|
if (index !== undefined) {
|
|
|
this.curIndex = index
|
|
this.curIndex = index
|
|
|
}
|
|
}
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: doPlay from playlist - song: ${item.name}, index: ${index}`);
|
|
|
} else {
|
|
} else {
|
|
|
|
|
+ // 点击文件列表中的歌曲,需要判断是否要创建新的播放列表
|
|
|
let globalVideoList = Utility.getGlobalList(this.videoLocalList, CommonConstants.TYPE_LOCAL) as VideoItem[];
|
|
let globalVideoList = Utility.getGlobalList(this.videoLocalList, CommonConstants.TYPE_LOCAL) as VideoItem[];
|
|
|
this.curIndex = Utility.getCurIndexFromGlobalList(globalVideoList, item.filePath)
|
|
this.curIndex = Utility.getCurIndexFromGlobalList(globalVideoList, item.filePath)
|
|
|
- this.songList = globalVideoList
|
|
|
|
|
-
|
|
|
|
|
- this.sonDataSource.pushArrayData(this.songList)
|
|
|
|
|
- this.currentSong = globalVideoList[this.curIndex]
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 检查当前播放列表是否包含这首歌
|
|
|
|
|
+ const currentSongIndex = this.songList.findIndex(song => song.filePath === item.filePath);
|
|
|
|
|
+
|
|
|
|
|
+ if (currentSongIndex >= 0 && ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
|
|
+ // 如果当前播放列表已经包含这首歌,使用现有播放列表
|
|
|
|
|
+ this.curIndex = currentSongIndex;
|
|
|
|
|
+ this.currentSong = this.songList[this.curIndex];
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: doPlay - Found song in existing playlist at index ${this.curIndex}, keeping playlist with ${this.songList.length} songs`);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果当前播放列表不包含这首歌,或者没有播放列表,则创建新的
|
|
|
|
|
+ this.songList = globalVideoList;
|
|
|
|
|
+ this.sonDataSource.pushArrayData(this.songList);
|
|
|
|
|
+ this.currentSong = globalVideoList[this.curIndex];
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: doPlay - Created new playlist with ${this.songList.length} songs, current song: ${this.currentSong.name}`);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
AppStorage.setOrCreate('currentSong',this.currentSong) ;
|
|
AppStorage.setOrCreate('currentSong',this.currentSong) ;
|
|
@@ -9795,8 +9805,19 @@ export struct LocalMusic {
|
|
|
private syncPlaylistToService() {
|
|
private syncPlaylistToService() {
|
|
|
try {
|
|
try {
|
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
|
|
+ // 记录调用栈信息以便调试
|
|
|
|
|
+ const stack = new Error().stack || 'No stack available';
|
|
|
|
|
+ const caller = stack.split('\n')[2] || 'Unknown caller';
|
|
|
|
|
+
|
|
|
this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
|
- LogUtils.getInstance().LOGI(`LocalMusic: Playlist synced to UnifiedPlayerService - ${this.songList.length} songs`);
|
|
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Playlist synced to UnifiedPlayerService - ${this.songList.length} songs, caller: ${caller.trim()}`);
|
|
|
|
|
+
|
|
|
|
|
+ // 如果同步的是小播放列表,记录更多信息
|
|
|
|
|
+ if (this.songList.length <= 20) {
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Small playlist sync detected, current path: ${this.currentPath}, first few songs: ${this.songList.slice(0, 5).map(s => s.name).join(', ')}`);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: syncPlaylistToService - No songs to sync');
|
|
|
}
|
|
}
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
LogUtils.getInstance().LOGI(`LocalMusic syncPlaylistToService error: ${error}`);
|
|
LogUtils.getInstance().LOGI(`LocalMusic syncPlaylistToService error: ${error}`);
|
|
@@ -10691,14 +10712,14 @@ export struct LocalMusic {
|
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
// 确保播放列表已同步到UnifiedPlayerService
|
|
// 确保播放列表已同步到UnifiedPlayerService
|
|
|
this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 使用UnifiedPlayerService播放下一首
|
|
// 使用UnifiedPlayerService播放下一首
|
|
|
await this.unifiedPlayerService.playNext();
|
|
await this.unifiedPlayerService.playNext();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 更新本地状态以保持UI同步
|
|
// 更新本地状态以保持UI同步
|
|
|
const currentIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
const currentIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
|
const currentSong = this.unifiedPlayerService.getCurrentSong();
|
|
const currentSong = this.unifiedPlayerService.getCurrentSong();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (currentSong) {
|
|
if (currentSong) {
|
|
|
this.curIndex = currentIndex;
|
|
this.curIndex = currentIndex;
|
|
|
this.currentSong = currentSong;
|
|
this.currentSong = currentSong;
|
|
@@ -10706,12 +10727,12 @@ export struct LocalMusic {
|
|
|
this.artist = currentSong.artist;
|
|
this.artist = currentSong.artist;
|
|
|
this.name = currentSong.name;
|
|
this.name = currentSong.name;
|
|
|
this.cover = currentSong.pixelMapPath;
|
|
this.cover = currentSong.pixelMapPath;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 同步到AppStorage,确保卡片能获取到最新状态
|
|
// 同步到AppStorage,确保卡片能获取到最新状态
|
|
|
- AppStorage.setOrCreate('songList', this.songList);
|
|
|
|
|
- AppStorage.setOrCreate('currIndex', this.curIndex);
|
|
|
|
|
- AppStorage.setOrCreate('currentSong', currentSong);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // AppStorage.setOrCreate('songList', this.songList);
|
|
|
|
|
+ // AppStorage.setOrCreate('currIndex', this.curIndex);
|
|
|
|
|
+ // AppStorage.setOrCreate('currentSong', currentSong);
|
|
|
|
|
+
|
|
|
this.changeImageAnimation();
|
|
this.changeImageAnimation();
|
|
|
|
|
|
|
|
}
|
|
}
|