fix: 修复mpv下的音量处理逻辑#739
Conversation
Summary of ChangesHello @lrst6963, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求修复了 MPV 音量处理逻辑中的一个关键问题。它实现了一种健壮的机制,以确保即使 MPV 播放器尚未完全初始化或连接,音量设置也能被正确地处理和应用。通过引入待处理音量状态并在连接就绪时应用它,可以有效防止音量命令在 MPV 启动阶段丢失,从而显著提高音量控制的可靠性和用户体验。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
| public setVolume(volume: number) { | ||
| // 记录待应用的初始音量 | ||
| this.pendingInitialVolume = volume; | ||
|
|
||
| // 尚未建立连接 | ||
| if (!this.isConnected || !this.client) { | ||
| if (this.mpvProcess) { | ||
| // 进程已启动但仍在连接中,交给 sendCommand 入队 | ||
| this.sendCommand("set_property", ["volume", volume]); | ||
| } else { | ||
| // 进程未启动,记录日志 | ||
| processLog.info(`记录待应用的 MPV 初始音量: ${volume}`); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| this.sendCommand("set_property", ["volume", volume]); | ||
| } |
There was a problem hiding this comment.
你好,这部分音量处理逻辑有些复杂和冗余,可能导致在特定情况下(进程已启动但未连接时)重复发送音量设置命令。当 setVolume 被调用时,它会设置 pendingInitialVolume 并调用 sendCommand 将命令入队。当连接建立后,flushQueue 会执行队列中的命令,紧接着 applyPendingVolume 会再次发送同一个音量命令,造成了重复。
为了简化逻辑并避免重复命令,建议重构 setVolume 方法,使其逻辑更清晰:
- 总是更新
pendingInitialVolume以记录最新的音量值。 - 仅当 MPV 已连接时才立即发送命令。
- 如果 MPV 进程尚未启动,则只记录日志,依赖后续连接成功后
applyPendingVolume来设置音量。 - 如果 MPV 正在启动但尚未连接,则不执行任何操作,同样等待
applyPendingVolume。
这样可以确保音量命令只被发送一次,且逻辑更加清晰。
public setVolume(volume: number) {
this.pendingInitialVolume = volume;
if (this.isConnected && this.client) {
// 如果已连接,立即发送命令
this.sendCommand("set_property", ["volume", volume]);
} else if (!this.mpvProcess) {
// 如果进程尚未启动,仅记录日志,等待启动后应用
processLog.info(`记录待应用的 MPV 初始音量: ${volume}`);
}
// 如果进程正在启动但未连接,则不执行任何操作,
// 等待连接成功后由 applyPendingVolume 处理。
}There was a problem hiding this comment.
Pull request overview
This PR fixes the volume handling logic in the MPV service to properly manage volume settings during the initialization and connection phases of the MPV process.
Changes:
- Introduced a
pendingInitialVolumeproperty to track volume settings before the MPV connection is fully established - Added
applyPendingVolume()method to apply any pending volume after successful connection - Enhanced
setVolume()method with connection state checking and proper queuing logic
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| private applyPendingVolume() { | ||
| if (!this.isConnected || !this.client) return; | ||
| if (this.pendingInitialVolume != null) { | ||
| const volume = this.pendingInitialVolume; |
There was a problem hiding this comment.
After applying the pending volume, consider clearing this.pendingInitialVolume to prevent reapplying the same volume value in subsequent reconnection scenarios. Otherwise, if the user changes the volume after connection, a reconnection might incorrectly reset it to the old pending value.
| const volume = this.pendingInitialVolume; | |
| const volume = this.pendingInitialVolume; | |
| this.pendingInitialVolume = null; |
| // 记录待应用的初始音量 | ||
| this.pendingInitialVolume = volume; | ||
|
|
||
| // 尚未建立连接 | ||
| if (!this.isConnected || !this.client) { |
There was a problem hiding this comment.
The pendingInitialVolume is unconditionally updated on every setVolume() call. This means that even after connection is established, this property will keep getting updated. Consider only setting pendingInitialVolume when the connection hasn't been established yet (!this.isConnected || !this.client), to avoid confusion about whether this represents a 'pending' or 'current' volume.
| // 记录待应用的初始音量 | |
| this.pendingInitialVolume = volume; | |
| // 尚未建立连接 | |
| if (!this.isConnected || !this.client) { | |
| // 尚未建立连接 | |
| if (!this.isConnected || !this.client) { | |
| // 记录待应用的初始音量 | |
| this.pendingInitialVolume = volume; |
No description provided.