|
|
@@ -1068,10 +1068,20 @@ export struct LocalMusic {
|
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
this.isFirstStartPlay = true
|
|
|
this.sonDataSource.pushArrayData(this.songList)
|
|
|
- if (this.currentSong === undefined && this.songList.length > 0) {
|
|
|
+
|
|
|
+ // 不要强制设置为第一首歌曲,保持从UnifiedPlayerService恢复的状态
|
|
|
+ // 只有在完全没有当前歌曲且没有有效索引时才设置默认值
|
|
|
+ if (this.currentSong === undefined && this.curIndex < 0 && this.songList.length > 0) {
|
|
|
this.isFirstStartPlay = false
|
|
|
+ this.curIndex = 0
|
|
|
this.currentSong = this.songList[0]
|
|
|
+ LogUtils.getInstance().LOGI('LocalMusic: mkDownLoadDir - No current song found, setting to first song as fallback');
|
|
|
+ } else if (this.currentSong === undefined && this.curIndex >= 0 && this.curIndex < this.songList.length) {
|
|
|
+ // 如果有有效索引但没有当前歌曲,从索引恢复
|
|
|
+ this.currentSong = this.songList[this.curIndex]
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: mkDownLoadDir - Restored current song from index ${this.curIndex}: ${this.currentSong.name}`);
|
|
|
}
|
|
|
+
|
|
|
if (this.currentSong) {
|
|
|
this.videoUrl = this.currentSong.filePath
|
|
|
this.name = this.currentSong.name
|
|
|
@@ -1079,7 +1089,7 @@ export struct LocalMusic {
|
|
|
AppStorage.setOrCreate('currentSong',this.currentSong);
|
|
|
}
|
|
|
|
|
|
- LogUtils.getInstance().LOGI(`LocalMusic: mkDownLoadDir - UI updated with existing playlist: ${this.songList.length} songs`);
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: mkDownLoadDir - UI updated with existing playlist: ${this.songList.length} songs, current index: ${this.curIndex}, current song: ${this.currentSong?.name || 'none'}`);
|
|
|
} else {
|
|
|
this.name = '空空如也'
|
|
|
LogUtils.getInstance().LOGI('LocalMusic: mkDownLoadDir - No playlist available, showing empty state');
|
|
|
@@ -1119,7 +1129,8 @@ export struct LocalMusic {
|
|
|
aboutToDisappear() {
|
|
|
console.info('LifeCycleComponent aboutToDisappear');
|
|
|
this.knockController?.immersiveDisableListening();
|
|
|
- this.curIndex = 0
|
|
|
+ // 移除 this.curIndex = 0,避免重置当前播放索引
|
|
|
+ // 保持当前播放状态,让UnifiedPlayerService管理播放状态的持久化
|
|
|
emitter.off(2);
|
|
|
emitter.off(101);
|
|
|
emitter.off(888);
|
|
|
@@ -9978,9 +9989,39 @@ export struct LocalMusic {
|
|
|
this.animationState = AnimationStatus.Running
|
|
|
LogUtils.getInstance().LOGI("startPlayOrResumePlay start this.CONTROL_PlayStatus:" + this.CONTROL_PlayStatus)
|
|
|
|
|
|
- // 确保播放列表已同步到UnifiedPlayerService
|
|
|
+ // 智能同步播放列表到UnifiedPlayerService
|
|
|
if (ArrayUtil.isNotEmpty(this.songList)) {
|
|
|
- this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
|
+ // 检查UnifiedPlayerService是否已经有播放列表
|
|
|
+ const servicePlaylist = this.unifiedPlayerService.getPlaylist();
|
|
|
+ const serviceIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
|
+
|
|
|
+ if (ArrayUtil.isEmpty(servicePlaylist)) {
|
|
|
+ // 如果服务没有播放列表,使用LocalMusic的播放列表
|
|
|
+ this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Set playlist to UnifiedPlayerService - ${this.songList.length} songs, index ${this.curIndex}`);
|
|
|
+ } else {
|
|
|
+ // 如果服务已有播放列表,同步服务的状态到LocalMusic
|
|
|
+ const serviceSong = this.unifiedPlayerService.getCurrentSong();
|
|
|
+ if (serviceSong && serviceIndex >= 0 && serviceIndex < servicePlaylist.length) {
|
|
|
+ this.songList = servicePlaylist;
|
|
|
+ this.curIndex = serviceIndex;
|
|
|
+ this.currentSong = serviceSong;
|
|
|
+ this.sonDataSource.pushArrayData(this.songList);
|
|
|
+
|
|
|
+ // 更新UI显示
|
|
|
+ this.videoUrl = serviceSong.filePath;
|
|
|
+ this.name = serviceSong.name;
|
|
|
+ this.artist = serviceSong.artist;
|
|
|
+ this.cover = serviceSong.pixelMapPath;
|
|
|
+
|
|
|
+ // 同步到AppStorage
|
|
|
+ AppStorage.setOrCreate('songList', this.songList);
|
|
|
+ AppStorage.setOrCreate('currIndex', this.curIndex);
|
|
|
+ AppStorage.setOrCreate('currentSong', this.currentSong);
|
|
|
+
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic: Synced from UnifiedPlayerService - ${this.songList.length} songs, index ${this.curIndex}, song: ${serviceSong.name}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 使用UnifiedPlayerService开始播放
|
|
|
@@ -11536,11 +11577,35 @@ export struct LocalMusic {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 确保播放列表已同步到UnifiedPlayerService
|
|
|
- this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
|
-
|
|
|
// 使用UnifiedPlayerService播放下一首
|
|
|
await this.unifiedPlayerService.playNext();
|
|
|
+ // 智能同步播放列表到UnifiedPlayerService
|
|
|
+ const servicePlaylist = this.unifiedPlayerService.getPlaylist();
|
|
|
+ const serviceIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
|
+
|
|
|
+ if (ArrayUtil.isEmpty(servicePlaylist)) {
|
|
|
+ // 如果服务没有播放列表,使用LocalMusic的播放列表
|
|
|
+ this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic playNext: Set playlist to UnifiedPlayerService - ${this.songList.length} songs, index ${this.curIndex}`);
|
|
|
+ } else {
|
|
|
+ // 如果服务已有播放列表,检查是否需要同步
|
|
|
+ const serviceSong = this.unifiedPlayerService.getCurrentSong();
|
|
|
+ if (serviceSong && serviceIndex >= 0 && serviceIndex < servicePlaylist.length) {
|
|
|
+ // 同步服务的状态到LocalMusic,确保状态一致
|
|
|
+ this.songList = servicePlaylist;
|
|
|
+ this.curIndex = serviceIndex;
|
|
|
+ this.currentSong = serviceSong;
|
|
|
+ this.sonDataSource.pushArrayData(this.songList);
|
|
|
+
|
|
|
+ // 同步到AppStorage
|
|
|
+ AppStorage.setOrCreate('songList', this.songList);
|
|
|
+ AppStorage.setOrCreate('currIndex', this.curIndex);
|
|
|
+ AppStorage.setOrCreate('currentSong', this.currentSong);
|
|
|
+
|
|
|
+ LogUtils.getInstance().LOGI(`LocalMusic playNext: Synced from UnifiedPlayerService - ${this.songList.length} songs, index ${this.curIndex}, song: ${serviceSong.name}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
// 更新本地状态以保持UI同步
|
|
|
const currentIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
|
@@ -11573,6 +11638,8 @@ export struct LocalMusic {
|
|
|
// 错误处理:回退到原有逻辑
|
|
|
this.fallbackPlayNext();
|
|
|
}
|
|
|
+ // 同步播放列表到UnifiedPlayerService
|
|
|
+ this.syncPlaylistToService();
|
|
|
}
|
|
|
|
|
|
// 回退到原有的playNext逻辑
|
|
|
@@ -11726,12 +11793,14 @@ export struct LocalMusic {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 确保播放列表已同步到UnifiedPlayerService
|
|
|
- this.unifiedPlayerService.setPlaylist(this.songList, this.curIndex);
|
|
|
-
|
|
|
// 使用UnifiedPlayerService播放上一首
|
|
|
await this.unifiedPlayerService.playPrevious();
|
|
|
|
|
|
+ // 智能同步播放列表到UnifiedPlayerService
|
|
|
+ const servicePlaylist = this.unifiedPlayerService.getPlaylist();
|
|
|
+ const serviceIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
|
+
|
|
|
+
|
|
|
// 更新本地状态以保持UI同步
|
|
|
const currentIndex = this.unifiedPlayerService.getCurrentIndex();
|
|
|
const currentSong = this.unifiedPlayerService.getCurrentSong();
|
|
|
@@ -11768,6 +11837,8 @@ export struct LocalMusic {
|
|
|
// 错误处理:回退到原有逻辑
|
|
|
this.fallbackPlayPrevious();
|
|
|
}
|
|
|
+ // 同步播放列表到UnifiedPlayerService
|
|
|
+ this.syncPlaylistToService();
|
|
|
}
|
|
|
|
|
|
// 回退到原有的playPrevious逻辑
|