213 lines
5.1 KiB
Markdown
213 lines
5.1 KiB
Markdown
# 远程连接音量自动调节器
|
||
|
||
自动检测 Windows 远程桌面 (RDP) 连接,并在连接建立时自动调整系统音量。
|
||
|
||
## 🎯 功能特性
|
||
|
||
- ✅ 自动检测 RDP 远程连接建立/断开
|
||
- ✅ 连接时自动降低音量(保护隐私/避免噪音)
|
||
- ✅ 断开时自动恢复音量(可选)
|
||
- ✅ 后台持续监控(守护进程模式)
|
||
- ✅ 可配置音量百分比和检测间隔
|
||
- ✅ 支持安装为 Windows 服务(开机自启)
|
||
- ✅ 详细日志记录
|
||
|
||
## 📦 安装步骤
|
||
|
||
### 1. 准备环境
|
||
|
||
确保目标 Windows 电脑已安装:
|
||
- Python 3.8 或更高版本
|
||
- pip 包管理器
|
||
|
||
### 2. 安装依赖
|
||
|
||
```bash
|
||
pip install pycaw comtypes wmi
|
||
```
|
||
|
||
或一键安装:
|
||
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
### 3. 配置文件
|
||
|
||
编辑 `config.ini`:
|
||
|
||
```ini
|
||
[volume]
|
||
remote_volume = 30 # 远程连接时音量 (0-100)
|
||
local_volume = 80 # 本地使用音量 (可选)
|
||
|
||
[monitor]
|
||
check_interval = 5 # 检测间隔 (秒)
|
||
|
||
[behavior]
|
||
adjust_on_connect = true
|
||
adjust_on_disconnect = true
|
||
```
|
||
|
||
### 4. 启动方式
|
||
|
||
#### 方式 A:手动启动(测试用)
|
||
|
||
双击运行 `启动监控.bat`
|
||
|
||
或在命令行:
|
||
```bash
|
||
python remote_volume_monitor.py --config config.ini
|
||
```
|
||
|
||
#### 方式 B:安装为 Windows 服务(推荐)
|
||
|
||
1. 下载 NSSM:https://nssm.cc/download
|
||
2. 解压到程序目录
|
||
3. 以**管理员身份**运行:
|
||
|
||
```bash
|
||
nssm install RemoteVolumeMonitor
|
||
```
|
||
|
||
4. 在弹出的界面中配置:
|
||
- **Path**: `C:\Python39\python.exe` (你的 Python 路径)
|
||
- **Args**: `C:\path\to\remote_volume_monitor.py --config C:\path\to\config.ini`
|
||
- **Startup directory**: `C:\path\to\`
|
||
|
||
5. 点击 "Install service"
|
||
|
||
6. 启动服务:
|
||
```bash
|
||
nssm start RemoteVolumeMonitor
|
||
```
|
||
|
||
#### 方式 C:使用提供的安装脚本
|
||
|
||
```bash
|
||
# 以管理员身份运行 CMD
|
||
python remote_volume_monitor.py --install-service
|
||
|
||
# 然后运行生成的 install_service.bat (管理员权限)
|
||
```
|
||
|
||
## 🔧 命令行参数
|
||
|
||
```bash
|
||
# 设置远程音量为 30%
|
||
python remote_volume_monitor.py --volume 30
|
||
|
||
# 使用配置文件
|
||
python remote_volume_monitor.py --config config.ini
|
||
|
||
# 创建示例配置文件
|
||
python remote_volume_monitor.py --create-config
|
||
|
||
# 测试模式(检测一次后退出)
|
||
python remote_volume_monitor.py --test
|
||
|
||
# 获取当前音量
|
||
python remote_volume_monitor.py --get-volume
|
||
|
||
# 立即设置音量
|
||
python remote_volume_monitor.py --set-volume 50
|
||
|
||
# 安装为 Windows 服务
|
||
python remote_volume_monitor.py --install-service
|
||
```
|
||
|
||
## 📊 日志查看
|
||
|
||
程序运行时会生成 `remote_volume.log` 文件,记录所有事件:
|
||
|
||
```
|
||
2026-03-07 17:30:00,123 - INFO - ✓ 音量控制器初始化成功
|
||
2026-03-07 17:30:00,456 - INFO - ✓ WMI 监控器初始化成功
|
||
2026-03-07 17:30:00,789 - INFO - 🚀 远程音量监控器已启动
|
||
2026-03-07 17:35:22,012 - INFO - 🔔 检测到远程连接建立
|
||
2026-03-07 17:35:22,345 - INFO - ✓ 音量已设置为 30%
|
||
2026-03-07 17:40:15,678 - INFO - 🔔 检测到远程连接断开
|
||
2026-03-07 17:40:15,901 - INFO - ✓ 音量已设置为 80%
|
||
```
|
||
|
||
## 🔍 工作原理
|
||
|
||
1. **会话检测**:通过 WMI 查询 `Win32_Session` 类,检测 `SessionType = "Remote"` 的活跃会话
|
||
2. **备用检测**:如果 WMI 不可用,使用环境变量 `SESSIONNAME` 和 `query user` 命令
|
||
3. **状态监控**:每 5 秒(可配置)检测一次会话状态变化
|
||
4. **音量控制**:使用 `pycaw` 库调用 Windows Core Audio API 调整主音量
|
||
|
||
## ⚠️ 注意事项
|
||
|
||
### 权限要求
|
||
- 需要普通用户权限即可运行
|
||
- 安装为服务时需要管理员权限
|
||
|
||
### 兼容性
|
||
- Windows 10/11
|
||
- Windows Server 2016/2019/2022
|
||
- 需要启用 WMI 服务
|
||
|
||
### 远程桌面类型
|
||
支持检测:
|
||
- ✅ Windows 远程桌面 (RDP)
|
||
- ✅ 快速助手 (Quick Assist)
|
||
- ⚠️ TeamViewer/AnyDesk 等第三方工具(可能无法检测,因为它们不使用 RDP 协议)
|
||
|
||
### 多用户场景
|
||
如果多人同时登录,程序会在任一远程会话活跃时调整音量。
|
||
|
||
## 🛠️ 故障排查
|
||
|
||
### 问题 1:依赖安装失败
|
||
```bash
|
||
# 尝试使用国内镜像
|
||
pip install pycaw comtypes wmi -i https://pypi.tuna.tsinghua.edu.cn/simple
|
||
```
|
||
|
||
### 问题 2:WMI 不可用
|
||
```bash
|
||
# 检查 WMI 服务状态
|
||
sc query winmgmt
|
||
|
||
# 如果未运行,启动服务
|
||
net start winmgmt
|
||
```
|
||
|
||
### 问题 3:音量无法调整
|
||
- 检查音频设备是否正常
|
||
- 确保程序有音频控制权限
|
||
- 尝试以管理员身份运行
|
||
|
||
### 问题 4:无法检测远程连接
|
||
- 检查防火墙是否阻止 WMI
|
||
- 确认远程桌面服务正在运行
|
||
- 查看日志文件获取详细错误信息
|
||
|
||
## 📝 自定义开发
|
||
|
||
如需支持其他远程工具(TeamViewer、向日葵等),可以修改 `RDPMonitor.is_remote_session()` 方法,添加对应的检测逻辑:
|
||
|
||
```python
|
||
def is_remote_session(self):
|
||
# 检测 TeamViewer
|
||
for proc in self.c.Win32_Process():
|
||
if 'teamviewer' in proc.Name.lower():
|
||
return True
|
||
|
||
# 检测向日葵
|
||
for proc in self.c.Win32_Process():
|
||
if 'sunlogin' in proc.Name.lower():
|
||
return True
|
||
|
||
# ... 其他检测方法
|
||
```
|
||
|
||
## 📄 许可证
|
||
|
||
MIT License - 自由使用和修改
|
||
|
||
## 🤝 贡献
|
||
|
||
欢迎提交 Issue 和 Pull Request!
|