Skip to content

Commit 6ef3c9c

Browse files
committed
fix: 修复mpv下的音量处理逻辑
1 parent d041fc5 commit 6ef3c9c

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

electron/main/services/MpvService.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class MpvService {
1515
private commandQueue: string[] = [];
1616
private observationMap: Map<number, string> = new Map();
1717
private currentAudioDevice: string = "auto";
18+
private pendingInitialVolume: number | null = null;
1819

1920
private playNonce = 0;
2021
private mpvProcessNonce: number | null = null;
@@ -137,6 +138,7 @@ export class MpvService {
137138
this.isConnected = true;
138139
this.setupEventListeners();
139140
this.flushQueue();
141+
this.applyPendingVolume();
140142
resolve();
141143
});
142144

@@ -194,6 +196,14 @@ export class MpvService {
194196
this.observeProperty(5, "duration");
195197
}
196198

199+
private applyPendingVolume() {
200+
if (!this.isConnected || !this.client) return;
201+
if (this.pendingInitialVolume != null) {
202+
const volume = this.pendingInitialVolume;
203+
this.sendCommand("set_property", ["volume", volume]);
204+
}
205+
}
206+
197207
private handleMpvEvent(event: any) {
198208
if (
199209
event.event === "property-change" ||
@@ -397,6 +407,21 @@ export class MpvService {
397407
}
398408

399409
public setVolume(volume: number) {
410+
// 记录待应用的初始音量
411+
this.pendingInitialVolume = volume;
412+
413+
// 尚未建立连接
414+
if (!this.isConnected || !this.client) {
415+
if (this.mpvProcess) {
416+
// 进程已启动但仍在连接中,交给 sendCommand 入队
417+
this.sendCommand("set_property", ["volume", volume]);
418+
} else {
419+
// 进程未启动,记录日志
420+
processLog.info(`记录待应用的 MPV 初始音量: ${volume}`);
421+
}
422+
return;
423+
}
424+
400425
this.sendCommand("set_property", ["volume", volume]);
401426
}
402427

0 commit comments

Comments
 (0)