Skip to content

fix: 修复mpv下的音量处理逻辑#739

Merged
imsyy merged 1 commit into
SPlayer-Dev:devfrom
lrst6963:dev
Jan 14, 2026
Merged

fix: 修复mpv下的音量处理逻辑#739
imsyy merged 1 commit into
SPlayer-Dev:devfrom
lrst6963:dev

Conversation

@lrst6963
Copy link
Copy Markdown
Contributor

No description provided.

@lrst6963 lrst6963 marked this pull request as ready for review January 12, 2026 18:38
Copilot AI review requested due to automatic review settings January 12, 2026 18:38
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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

  • 音量暂存机制: 引入了 pendingInitialVolume 属性,用于在 MPV 客户端连接就绪前临时存储音量设置。
  • 连接后应用音量: 确保在 MPV 客户端连接建立后,通过调用 applyPendingVolume 方法,将之前暂存的音量设置应用到播放器。
  • 改进 setVolume 方法: 修改了 setVolume 方法,使其在 MPV 尚未连接时,能够智能地将音量设置命令加入队列或记录,以防止音量设置丢失。
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 旨在修复 mpv 在完全连接前调用 setVolume 时的音量处理逻辑。通过引入 pendingInitialVolume 属性来缓存音量值,并在连接成功后应用,这个方向是正确的。然而,setVolume 方法的当前实现存在一些逻辑冗余,在 mpv 正在连接但尚未成功时,会导致音量设置命令被重复发送。我的审查意见中提供了一个重构建议,以简化 setVolume 的逻辑并修复此问题,使代码更健壮和清晰。

Comment on lines 409 to 426
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]);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

你好,这部分音量处理逻辑有些复杂和冗余,可能导致在特定情况下(进程已启动但未连接时)重复发送音量设置命令。当 setVolume 被调用时,它会设置 pendingInitialVolume 并调用 sendCommand 将命令入队。当连接建立后,flushQueue 会执行队列中的命令,紧接着 applyPendingVolume 会再次发送同一个音量命令,造成了重复。

为了简化逻辑并避免重复命令,建议重构 setVolume 方法,使其逻辑更清晰:

  1. 总是更新 pendingInitialVolume 以记录最新的音量值。
  2. 仅当 MPV 已连接时才立即发送命令。
  3. 如果 MPV 进程尚未启动,则只记录日志,依赖后续连接成功后 applyPendingVolume 来设置音量。
  4. 如果 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 处理。
  }

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 pendingInitialVolume property 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;
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
const volume = this.pendingInitialVolume;
const volume = this.pendingInitialVolume;
this.pendingInitialVolume = null;

Copilot uses AI. Check for mistakes.
Comment on lines +410 to +414
// 记录待应用的初始音量
this.pendingInitialVolume = volume;

// 尚未建立连接
if (!this.isConnected || !this.client) {
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// 记录待应用的初始音量
this.pendingInitialVolume = volume;
// 尚未建立连接
if (!this.isConnected || !this.client) {
// 尚未建立连接
if (!this.isConnected || !this.client) {
// 记录待应用的初始音量
this.pendingInitialVolume = volume;

Copilot uses AI. Check for mistakes.
@imsyy imsyy merged commit 9fb2248 into SPlayer-Dev:dev Jan 14, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants