|
@@ -477,7 +477,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
private isUpdatingForms: boolean = false;
|
|
private isUpdatingForms: boolean = false;
|
|
|
private lastAvSessionUpdateTime: number = 0;
|
|
private lastAvSessionUpdateTime: number = 0;
|
|
|
private progressTimer: number = -1;
|
|
private progressTimer: number = -1;
|
|
|
- private isInitialized: boolean = false;
|
|
|
|
|
|
|
+ private initialized: boolean = false;
|
|
|
private isDataRestored: boolean = false; // 新增:数据恢复完成标识
|
|
private isDataRestored: boolean = false; // 新增:数据恢复完成标识
|
|
|
|
|
|
|
|
private isPlayerPreparedForCurrentSong: boolean = false; // 新增:播放器是否已为当前歌曲准备就绪
|
|
private isPlayerPreparedForCurrentSong: boolean = false; // 新增:播放器是否已为当前歌曲准备就绪
|
|
@@ -522,7 +522,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async initialize(context: common.UIAbilityContext): Promise<void> {
|
|
async initialize(context: common.UIAbilityContext): Promise<void> {
|
|
|
- if (this.isInitialized && this.context === context) {
|
|
|
|
|
|
|
+ if (this.initialized && this.context === context) {
|
|
|
LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService已经初始化过了');
|
|
LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService已经初始化过了');
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -550,7 +550,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
// 设置状态模型监听器,自动广播状态变化
|
|
// 设置状态模型监听器,自动广播状态变化
|
|
|
|
|
|
|
|
this.setupProgressTimer();
|
|
this.setupProgressTimer();
|
|
|
- this.isInitialized = true;
|
|
|
|
|
|
|
+ this.initialized = true;
|
|
|
|
|
|
|
|
// 异步初始化耗时组件,避免阻塞
|
|
// 异步初始化耗时组件,避免阻塞
|
|
|
this.initializeHeavyComponentsAsync(context);
|
|
this.initializeHeavyComponentsAsync(context);
|
|
@@ -660,14 +660,20 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
try {
|
|
try {
|
|
|
console.log("Heanup2 UnifiedPlayerService: 开始恢复播放");
|
|
console.log("Heanup2 UnifiedPlayerService: 开始恢复播放");
|
|
|
|
|
|
|
|
- // 统一的服务就绪检查 - 这是最核心的检查
|
|
|
|
|
|
|
+ // 优化的服务就绪检查 - 支持冷启动场景
|
|
|
if (!this.isAllServicesReady()) {
|
|
if (!this.isAllServicesReady()) {
|
|
|
console.log("Heanup2 UnifiedPlayerService: 服务未就绪,等待服务初始化完成");
|
|
console.log("Heanup2 UnifiedPlayerService: 服务未就绪,等待服务初始化完成");
|
|
|
- const isReady = await this.waitForAllServicesReady(3000, false);
|
|
|
|
|
|
|
+ const isReady = await this.waitForAllServicesReady(5000, true); // 增加等待时间并启用数据恢复检查
|
|
|
if (!isReady) {
|
|
if (!isReady) {
|
|
|
- const error = new Error('Services not ready for playback');
|
|
|
|
|
- await this.handlePlaybackError(error, PlayerErrorType.INITIALIZATION_ERROR);
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ console.log("Heanup2 UnifiedPlayerService: 服务等待超时,尝试强制数据恢复...");
|
|
|
|
|
+ // 尝试强制数据恢复作为最后手段
|
|
|
|
|
+ const forceRestored = await this.forceDataRestoration();
|
|
|
|
|
+ if (!forceRestored) {
|
|
|
|
|
+ const error = new Error('Services not ready for playback after force restoration');
|
|
|
|
|
+ await this.handlePlaybackError(error, PlayerErrorType.INITIALIZATION_ERROR);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ console.log("Heanup2 UnifiedPlayerService: 强制数据恢复成功,继续播放流程");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -689,10 +695,11 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
|
|
|
|
|
if (!currentSong) {
|
|
if (!currentSong) {
|
|
|
console.log("Heanup2 UnifiedPlayerService: 恢复后仍然没有可播放的歌曲");
|
|
console.log("Heanup2 UnifiedPlayerService: 恢复后仍然没有可播放的歌曲");
|
|
|
- await this.handlePlaybackError(new Error('No song to play'), PlayerErrorType.PLAYBACK_ERROR);
|
|
|
|
|
|
|
+ // 冷启动优化:播放列表为空时的友好处理
|
|
|
|
|
+ await this.handleEmptyPlaylistScenario();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- console.log("Heanup2 UnifiedPlayerService: 恢复成功,当前歌曲信息:" + json.stringify(currentSong));
|
|
|
|
|
|
|
+ console.log("Heanup2 UnifiedPlayerService: 恢复成功,当前歌曲信息:" + JSON.stringify(currentSong));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
this.stateModel.updateLoadingState(true);
|
|
this.stateModel.updateLoadingState(true);
|
|
@@ -719,6 +726,74 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 处理空播放列表场景(冷启动优化)
|
|
|
|
|
+ */
|
|
|
|
|
+ private async handleEmptyPlaylistScenario(): Promise<void> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ console.log("Heanup2 UnifiedPlayerService: 处理空播放列表场景");
|
|
|
|
|
+
|
|
|
|
|
+ // 停止加载状态
|
|
|
|
|
+ this.stateModel.updateLoadingState(false);
|
|
|
|
|
+ this.stateModel.updatePlayingState(false);
|
|
|
|
|
+
|
|
|
|
|
+ // 尝试从数据库快速加载一些歌曲(如果有)
|
|
|
|
|
+ const quickSongs = await this.loadQuickPlaylistFromDatabase();
|
|
|
|
|
+ if (quickSongs && quickSongs.length > 0) {
|
|
|
|
|
+ console.log(`Heanup2 UnifiedPlayerService: 从数据库快速加载${quickSongs.length}首歌曲`);
|
|
|
|
|
+ this.setPlaylist(quickSongs, 0);
|
|
|
|
|
+
|
|
|
|
|
+ // 再次尝试播放
|
|
|
|
|
+ const currentSong = this.playlistModel.getCurrentSong();
|
|
|
|
|
+ if (currentSong) {
|
|
|
|
|
+ await this.validateSongForPlayback(currentSong);
|
|
|
|
|
+ await this.setupAndStartPlayback(currentSong);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果仍然没有歌曲,提供友好的用户提示
|
|
|
|
|
+ console.log("Heanup2 UnifiedPlayerService: 无可播放的歌曲,等待用户添加歌曲");
|
|
|
|
|
+
|
|
|
|
|
+ // 更新widget显示空状态
|
|
|
|
|
+ await this.updateAllForms();
|
|
|
|
|
+
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.log(`Heanup2 UnifiedPlayerService: 处理空播放列表场景失败: ${error}`);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从数据库快速加载播放列表(用于冷启动场景)
|
|
|
|
|
+ */
|
|
|
|
|
+ private async loadQuickPlaylistFromDatabase(): Promise<VideoItem[] | null> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (!this.context) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 尝试从数据库加载最近播放的歌曲
|
|
|
|
|
+ const table = this.getTable();
|
|
|
|
|
+ return new Promise<VideoItem[]>((resolve) => {
|
|
|
|
|
+ table.queryRecentPlayedRecords(10, (songs: VideoItem[]) => {
|
|
|
|
|
+ if (songs && songs.length > 0) {
|
|
|
|
|
+ console.log(`Heanup2 UnifiedPlayerService: 从数据库加载到${songs.length}首最近播放的歌曲`);
|
|
|
|
|
+ resolve(songs);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果没有最近播放,尝试加载任意歌曲
|
|
|
|
|
+ table.query(0, (allSongs: VideoItem[]) => {
|
|
|
|
|
+ console.log(`Heanup2 UnifiedPlayerService: 从数据库加载到${allSongs?.length || 0}首所有歌曲`);
|
|
|
|
|
+ resolve(allSongs || []);
|
|
|
|
|
+ }, true);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.log(`Heanup2 UnifiedPlayerService: 从数据库加载播放列表失败: ${error}`);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 检查是否可以从暂停状态恢复播放
|
|
* 检查是否可以从暂停状态恢复播放
|
|
|
*/
|
|
*/
|
|
@@ -763,8 +838,8 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
this.startProgressTimer();
|
|
this.startProgressTimer();
|
|
|
this.updateSessionPlayState();
|
|
this.updateSessionPlayState();
|
|
|
this.updateWidgetsForStateChange();
|
|
this.updateWidgetsForStateChange();
|
|
|
- // 同步更新所有桌面卡片状态
|
|
|
|
|
- this.updateAllForms();
|
|
|
|
|
|
|
+ // 关键修复:使用强制更新确保状态同步时卡片立即更新
|
|
|
|
|
+ this.updateAllForms(true);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async toggleFavorite(): Promise<void> {
|
|
public async toggleFavorite(): Promise<void> {
|
|
@@ -810,8 +885,8 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
this.updateSessionPlayState();
|
|
this.updateSessionPlayState();
|
|
|
await this.updateWidgetsForPlayStateChange();
|
|
await this.updateWidgetsForPlayStateChange();
|
|
|
|
|
|
|
|
- // 同步更新所有桌面卡片状态
|
|
|
|
|
- await this.updateAllForms();
|
|
|
|
|
|
|
+ // 关键修复:使用强制更新确保卡片状态立即同步
|
|
|
|
|
+ await this.updateAllForms(true);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
throw new Error(`Resume from pause failed: ${error}`);
|
|
throw new Error(`Resume from pause failed: ${error}`);
|
|
|
}
|
|
}
|
|
@@ -947,8 +1022,8 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
// 更新卡片显示暂停状态
|
|
// 更新卡片显示暂停状态
|
|
|
await this.updateWidgetsForPlayStateChange();
|
|
await this.updateWidgetsForPlayStateChange();
|
|
|
|
|
|
|
|
- // 同步更新所有桌面卡片状态
|
|
|
|
|
- await this.updateAllForms();
|
|
|
|
|
|
|
+ // 关键修复:使用强制更新确保卡片状态立即同步
|
|
|
|
|
+ await this.updateAllForms(true);
|
|
|
|
|
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService pause error: ${error}`);
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService pause error: ${error}`);
|
|
@@ -1387,6 +1462,13 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 检查服务是否已初始化
|
|
|
|
|
+ */
|
|
|
|
|
+ isServiceInitialized(): boolean {
|
|
|
|
|
+ return this.initialized;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 检查所有相关服务是否已准备就绪
|
|
* 检查所有相关服务是否已准备就绪
|
|
|
* 用于桌面卡片控制等场景,确保在执行播放控制前服务状态正常
|
|
* 用于桌面卡片控制等场景,确保在执行播放控制前服务状态正常
|
|
@@ -1398,7 +1480,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
this.hasChecked = true;
|
|
this.hasChecked = true;
|
|
|
try {
|
|
try {
|
|
|
// 检查基本初始化状态
|
|
// 检查基本初始化状态
|
|
|
- if (!this.isInitialized) {
|
|
|
|
|
|
|
+ if (!this.initialized) {
|
|
|
LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService: Service not initialized');
|
|
LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService: Service not initialized');
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
@@ -1442,7 +1524,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
// 检查基础初始化
|
|
// 检查基础初始化
|
|
|
- if (!this.isInitialized) {
|
|
|
|
|
|
|
+ if (!this.initialized) {
|
|
|
errors.push('Service not initialized');
|
|
errors.push('Service not initialized');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1518,7 +1600,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
const currentIndex = this.playlistModel?.getCurrentIndex() || -1;
|
|
const currentIndex = this.playlistModel?.getCurrentIndex() || -1;
|
|
|
|
|
|
|
|
const details: ServiceReadinessDetails = {
|
|
const details: ServiceReadinessDetails = {
|
|
|
- initialized: this.isInitialized,
|
|
|
|
|
|
|
+ initialized: this.initialized,
|
|
|
hasPlayerInstance: !!playerInstance,
|
|
hasPlayerInstance: !!playerInstance,
|
|
|
hasContext: !!this.context,
|
|
hasContext: !!this.context,
|
|
|
dataRestored: this.isDataRestored,
|
|
dataRestored: this.isDataRestored,
|
|
@@ -1626,8 +1708,10 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
const currentSong = this.playlistModel.getCurrentSong();
|
|
const currentSong = this.playlistModel.getCurrentSong();
|
|
|
this.stateModel.updateCurrentSong(currentSong);
|
|
this.stateModel.updateCurrentSong(currentSong);
|
|
|
|
|
|
|
|
- // 同步到持久化存储
|
|
|
|
|
- this.syncPlaylistToStorage();
|
|
|
|
|
|
|
+ // 同步到持久化存储(异步执行,不阻塞主线程)
|
|
|
|
|
+ this.syncPlaylistToStorage().catch((error:Error) => {
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService setPlaylist syncPlaylistToStorage error: ${error}`);
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
// 更新卡片显示播放列表变化
|
|
// 更新卡片显示播放列表变化
|
|
|
this.updateWidgetsForPlaylistChange();
|
|
this.updateWidgetsForPlaylistChange();
|
|
@@ -1648,8 +1732,10 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
// 更新状态模型中的播放列表状态
|
|
// 更新状态模型中的播放列表状态
|
|
|
this.updatePlaylistStateInModelSync();
|
|
this.updatePlaylistStateInModelSync();
|
|
|
|
|
|
|
|
- // 同步到持久化存储
|
|
|
|
|
- this.syncPlaylistToStorage();
|
|
|
|
|
|
|
+ // 同步到持久化存储(异步执行,不阻塞主线程)
|
|
|
|
|
+ this.syncPlaylistToStorage().catch((error:Error) => {
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService addToPlaylist syncPlaylistToStorage error: ${error}`);
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
// 更新卡片显示播放列表变化
|
|
// 更新卡片显示播放列表变化
|
|
|
this.updateWidgetsForPlaylistChange();
|
|
this.updateWidgetsForPlaylistChange();
|
|
@@ -1669,8 +1755,10 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
const currentSong = this.playlistModel.getCurrentSong();
|
|
const currentSong = this.playlistModel.getCurrentSong();
|
|
|
this.stateModel.updateCurrentSong(currentSong);
|
|
this.stateModel.updateCurrentSong(currentSong);
|
|
|
|
|
|
|
|
- // 同步到持久化存储
|
|
|
|
|
- this.syncPlaylistToStorage();
|
|
|
|
|
|
|
+ // 同步到持久化存储(异步执行,不阻塞主线程)
|
|
|
|
|
+ this.syncPlaylistToStorage().catch((error:Error) => {
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService removeFromPlaylist syncPlaylistToStorage error: ${error}`);
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
// 更新卡片显示播放列表变化
|
|
// 更新卡片显示播放列表变化
|
|
|
this.updateWidgetsForPlaylistChange();
|
|
this.updateWidgetsForPlaylistChange();
|
|
@@ -1720,7 +1808,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 清理资源
|
|
// 清理资源
|
|
|
- release(): void {
|
|
|
|
|
|
|
+ async release(): Promise<void> {
|
|
|
try {
|
|
try {
|
|
|
this.stopProgressTimer();
|
|
this.stopProgressTimer();
|
|
|
|
|
|
|
@@ -1734,6 +1822,11 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
this.avMetadataUpdateTimer = -1;
|
|
this.avMetadataUpdateTimer = -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 关键修复:先保存当前状态,再清理资源
|
|
|
|
|
+ // 这样可以确保播放列表在应用被杀掉时不会丢失
|
|
|
|
|
+ await this.saveCurrentState();
|
|
|
|
|
+ LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService: Current state saved before resource cleanup');
|
|
|
|
|
+
|
|
|
// 重要修复:确保在应用销毁时将播放状态设置为停止
|
|
// 重要修复:确保在应用销毁时将播放状态设置为停止
|
|
|
try {
|
|
try {
|
|
|
// 强制设置为停止状态,防止卡片显示错误的播放状态
|
|
// 强制设置为停止状态,防止卡片显示错误的播放状态
|
|
@@ -1750,9 +1843,6 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Failed to force stop state: ${error}`);
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Failed to force stop state: ${error}`);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 保存当前状态(现在应该是停止状态)
|
|
|
|
|
- this.saveCurrentState();
|
|
|
|
|
-
|
|
|
|
|
if (this.avSessionController) {
|
|
if (this.avSessionController) {
|
|
|
this.avSessionController.unregisterSessionListener();
|
|
this.avSessionController.unregisterSessionListener();
|
|
|
this.avSessionController = null;
|
|
this.avSessionController = null;
|
|
@@ -1762,8 +1852,9 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
this.playlistSync.removeSyncListener(this);
|
|
this.playlistSync.removeSyncListener(this);
|
|
|
this.playlistSync.release();
|
|
this.playlistSync.release();
|
|
|
|
|
|
|
|
|
|
+ // 最后清理播放列表模型,确保在此之前状态已经保存
|
|
|
this.playlistModel.clear();
|
|
this.playlistModel.clear();
|
|
|
- this.isInitialized = false;
|
|
|
|
|
|
|
+ this.initialized = false;
|
|
|
|
|
|
|
|
LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService: Resources released');
|
|
LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService: Resources released');
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
@@ -1912,7 +2003,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
} catch (recoveryError) {
|
|
} catch (recoveryError) {
|
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Error recovery failed: ${recoveryError}`);
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Error recovery failed: ${recoveryError}`);
|
|
|
this.stateModel.notifyError(new PlayerError(PlayerErrorType.PLAYBACK_ERROR,
|
|
this.stateModel.notifyError(new PlayerError(PlayerErrorType.PLAYBACK_ERROR,
|
|
|
- `Recovery failed: ${recoveryError.message}`));
|
|
|
|
|
|
|
+ `Recovery failed: ${(recoveryError as Error).message}`));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -2454,7 +2545,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
/**
|
|
/**
|
|
|
* 保存当前状态
|
|
* 保存当前状态
|
|
|
*/
|
|
*/
|
|
|
- private saveCurrentState(): void {
|
|
|
|
|
|
|
+ private async saveCurrentState(): Promise<void> {
|
|
|
try {
|
|
try {
|
|
|
// 获取当前状态
|
|
// 获取当前状态
|
|
|
const currentState = this.stateModel.getState();
|
|
const currentState = this.stateModel.getState();
|
|
@@ -2497,7 +2588,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this.syncPlaylistToStorage();
|
|
|
|
|
|
|
+ await this.syncPlaylistToStorage();
|
|
|
|
|
|
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Current state saved successfully`);
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Current state saved successfully`);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
@@ -2508,20 +2599,30 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
/**
|
|
/**
|
|
|
* 外部调用保存当前状态的方法(供EntryAbility使用)
|
|
* 外部调用保存当前状态的方法(供EntryAbility使用)
|
|
|
*/
|
|
*/
|
|
|
- public saveCurrentStateExternal(): void {
|
|
|
|
|
- this.saveCurrentState();
|
|
|
|
|
|
|
+ public async saveCurrentStateExternal(): Promise<void> {
|
|
|
|
|
+ await this.saveCurrentState();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 同步播放列表到存储
|
|
* 同步播放列表到存储
|
|
|
*/
|
|
*/
|
|
|
- private syncPlaylistToStorage(): void {
|
|
|
|
|
|
|
+ private async syncPlaylistToStorage(): Promise<void> {
|
|
|
try {
|
|
try {
|
|
|
const playlist = this.playlistModel.getSongs();
|
|
const playlist = this.playlistModel.getSongs();
|
|
|
const currentIndex = this.playlistModel.getCurrentIndex();
|
|
const currentIndex = this.playlistModel.getCurrentIndex();
|
|
|
const playMode = this.stateModel.getState().playMode;
|
|
const playMode = this.stateModel.getState().playMode;
|
|
|
|
|
|
|
|
- this.playlistSync.syncPlaylistToStorage(playlist, currentIndex, playMode);
|
|
|
|
|
|
|
+ // 获取调用栈信息用于调试
|
|
|
|
|
+ const stack = new Error().stack;
|
|
|
|
|
+ const caller = stack?.split('\n')[2]?.trim() || 'Unknown caller';
|
|
|
|
|
+
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: syncPlaylistToStorage called - playlist length: ${playlist?.length || 0}, currentIndex: ${currentIndex}, playMode: ${playMode}, caller: ${caller}`);
|
|
|
|
|
+
|
|
|
|
|
+ // 总是尝试保存播放列表状态,包括空播放列表的情况
|
|
|
|
|
+ // 这样可以确保用户关闭应用时的真实状态被正确保存
|
|
|
|
|
+ await this.playlistSync.syncPlaylistToStorage(playlist || [], currentIndex, playMode);
|
|
|
|
|
+
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: syncPlaylistToStorage completed successfully`);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService syncPlaylistToStorage error: ${error}`);
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService syncPlaylistToStorage error: ${error}`);
|
|
|
}
|
|
}
|
|
@@ -2574,8 +2675,10 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
*/
|
|
*/
|
|
|
onAutoSyncPerformed(): void {
|
|
onAutoSyncPerformed(): void {
|
|
|
try {
|
|
try {
|
|
|
- // 定期保存当前状态
|
|
|
|
|
- this.saveCurrentState();
|
|
|
|
|
|
|
+ // 定期保存当前状态(异步执行,不等待完成)
|
|
|
|
|
+ this.saveCurrentState().catch((error:Error) => {
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService onAutoSyncPerformed saveCurrentState error: ${error}`);
|
|
|
|
|
+ });
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService onAutoSyncPerformed error: ${error}`);
|
|
LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService onAutoSyncPerformed error: ${error}`);
|
|
|
}
|
|
}
|
|
@@ -2750,7 +2853,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
.LOGI('Heanup UnifiedPlayerService: Failed to set up audio interrupt listener - no player instance');
|
|
.LOGI('Heanup UnifiedPlayerService: Failed to set up audio interrupt listener - no player instance');
|
|
|
}
|
|
}
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
- LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Error setting up audio interrupt listener: ${error.message}`);
|
|
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Error setting up audio interrupt listener: ${(error as Error).message}`);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -2803,7 +2906,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
await this.broadcastCurrentState();
|
|
await this.broadcastCurrentState();
|
|
|
|
|
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
- LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Error handling audio interrupt: ${error.message}`);
|
|
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService: Error handling audio interrupt: ${(error as Error).message}`);
|
|
|
await this.handlePlaybackError(error as Error, PlayerErrorType.AUDIO_INTERRUPT_ERROR);
|
|
await this.handlePlaybackError(error as Error, PlayerErrorType.AUDIO_INTERRUPT_ERROR);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -3254,10 +3357,11 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
|
|
|
* 将VideoItem和PlayerState数据传入桌面卡片,控制卡片的显示
|
|
* 将VideoItem和PlayerState数据传入桌面卡片,控制卡片的显示
|
|
|
*/
|
|
*/
|
|
|
async updateAllForms(forceUpdate: boolean = false) {
|
|
async updateAllForms(forceUpdate: boolean = false) {
|
|
|
- // 增强防抖保护,特别是在APP前台运行时
|
|
|
|
|
|
|
+ // 优化防抖逻辑:缩短防抖时间,提高响应速度
|
|
|
const now = Date.now();
|
|
const now = Date.now();
|
|
|
- if (!forceUpdate && this.lastFormUpdateTime && (now - this.lastFormUpdateTime < 200)) {
|
|
|
|
|
- LogUtils.getInstance().LOGI('Heanup UnifiedPlayerService updateAllForms: Skipping due to debounce protection (200ms)');
|
|
|
|
|
|
|
+ const debounceTime = forceUpdate ? 0 : 100; // 强制更新无防抖,普通更新减少到100ms
|
|
|
|
|
+ if (!forceUpdate && this.lastFormUpdateTime && (now - this.lastFormUpdateTime < debounceTime)) {
|
|
|
|
|
+ LogUtils.getInstance().LOGI(`Heanup UnifiedPlayerService updateAllForms: Skipping due to debounce protection (${debounceTime}ms)`);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|