Skip to content

Commit c6373af

Browse files
committed
feat: 添加系统休眠抑制 fixed #191
1 parent aabe0ea commit c6373af

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎electron/main/ipc/player.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { appName, getSongCacheDir } from "@main/utils/config";
3232
import * as songCache from "@main/services/songCache";
3333
import { parseArtists, parseAlbum, formatArtists, artistNames } from "@main/utils/metadata";
3434
import { playerLog } from "@main/utils/logger";
35+
import { updatePowerBlocker, releasePowerBlocker } from "@main/utils/powerBlocker";
3536
import { ErrorCode } from "@shared/types/errors";
3637
import type {
3738
Artist,
@@ -116,6 +117,8 @@ const registerNativeEvents = (inst: InstanceType<AudioEngineModule["AudioPlayer"
116117
switch (event.type) {
117118
case "stateChanged": {
118119
const state = (event.state ?? "idle") as PlayerState;
120+
// 播放中阻止系统休眠,暂停/停止时释放唤醒锁,保证系统可正常休眠
121+
updatePowerBlocker(state === "playing" || state === "loading");
119122
// 更新缩略图工具栏和托盘菜单
120123
getThumbar()?.updateThumbar(state === "playing");
121124
setTrayPlayState(state === "playing" ? "playing" : "paused");
@@ -784,6 +787,9 @@ export const registerPlayerIpc = (): void => {
784787
wsBroadcast(stoppedEvent);
785788
};
786789
powerMonitor.on("resume", resumeHandler);
787-
// 退出前停止设备监听
788-
app.on("before-quit", stopDeviceMonitoring);
790+
// 退出前停止设备监听并释放休眠抑制
791+
app.on("before-quit", () => {
792+
stopDeviceMonitoring();
793+
releasePowerBlocker();
794+
});
789795
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { powerSaveBlocker } from "electron";
2+
import { systemLog } from "./logger";
3+
4+
let blockerId: number | null = null;
5+
6+
/**
7+
* 按播放状态更新系统休眠抑制
8+
*
9+
* 播放中(含缓冲 loading)通过 prevent-app-suspension 阻止系统睡眠,
10+
* 其它状态立即释放,避免空闲时持续占用唤醒锁导致无法休眠
11+
* @param active - 是否有正在进行的播放
12+
*/
13+
export const updatePowerBlocker = (active: boolean): void => {
14+
if (active && blockerId === null) {
15+
blockerId = powerSaveBlocker.start("prevent-app-suspension");
16+
systemLog.info(`已启用休眠抑制 (id: ${blockerId})`);
17+
} else if (!active && blockerId !== null) {
18+
powerSaveBlocker.stop(blockerId);
19+
systemLog.info(`已释放休眠抑制 (id: ${blockerId})`);
20+
blockerId = null;
21+
}
22+
};
23+
24+
/** 释放休眠抑制(应用退出前调用) */
25+
export const releasePowerBlocker = (): void => updatePowerBlocker(false);

0 commit comments

Comments
 (0)