本文目录导读:

- Windows - PowerShell 脚本 (音频设备切换)
- Linux - ALSA/PulseAudio 脚本
- macOS - SwitchAudioSource 脚本
- 通用 - AutoHotkey 快捷键 (Windows)
- 获取设备ID的方法
- 创建桌面快捷方式
- 推荐使用方案
Windows - PowerShell 脚本 (音频设备切换)
# audio-switch.ps1
param (
[Parameter(Mandatory=$true)]
[ValidateSet("speakers", "headphones", "bluetooth")]
[string]$Preset
)
# 音频设备ID(需要先通过Get-AudioDevice获取)
$devices = @{
speakers = "{0.0.0.00000000}.{your-speaker-id}"
headphones = "{0.0.0.00000000}.{your-headphone-id}"
bluetooth = "{0.0.0.00000000}.{your-bt-id}"
}
if ($devices.ContainsKey($Preset)) {
# 使用AudioDeviceCmdlets模块(需先安装)
# Install-Module -Name AudioDeviceCmdlets
Set-AudioDevice -ID $devices[$Preset]
Write-Host "切换到: $Preset" -ForegroundColor Green
} else {
Write-Host "预设不存在" -ForegroundColor Red
}
使用方法:
.\audio-switch.ps1 -Preset headphones
Linux - ALSA/PulseAudio 脚本
#!/bin/bash
# audio-preset.sh
case "$1" in
speakers)
pactl set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo
;;
headphones)
pactl set-default-sink alsa_output.usb-Logitech_G533_Gaming_Headset-00.analog-stereo
;;
bluetooth)
pactl set-default-sink bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink
;;
*)
echo "用法: $0 {speakers|headphones|bluetooth}"
exit 1
;;
esac
# 同时切换默认输入源(可选)
if [ "$1" = "headphones" ]; then
pactl set-default-source alsa_input.usb-Logitech_G533_Gaming_Headset-00.mono-fallback
fi
echo "音频已切换到: $1"
使用方法:
chmod +x audio-preset.sh ./audio-preset.sh speakers
macOS - SwitchAudioSource 脚本
#!/bin/bash
# audio-preset-mac.sh
# 需要先安装 SwitchAudioSource
# brew install switchaudio-osx
case "$1" in
speakers)
SwitchAudioSource -f "MacBook Pro Speakers"
;;
headphones)
SwitchAudioSource -f "External Headphones"
;;
bluetooth)
SwitchAudioSource -f "Sony WH-1000XM4"
;;
*)
echo "用法: $0 {speakers|headphones|bluetooth}"
exit 1
;;
esac
# 可同时调整音量
osascript -e "set volume output volume 70"
echo "音频已切换到: $1"
通用 - AutoHotkey 快捷键 (Windows)
; audio-presets.ahk
^!1:: ; Ctrl+Alt+1 切换到音箱
Run, powershell.exe -Command "Set-AudioDevice -ID '{音箱设备ID}'"
return
^!2:: ; Ctrl+Alt+2 切换到耳机
Run, powershell.exe -Command "Set-AudioDevice -ID '{耳机设备ID}'"
return
^!3:: ; Ctrl+Alt+3 切换到蓝牙
Run, powershell.exe -Command "Set-AudioDevice -ID '{蓝牙设备ID}'"
return
; 显示当前设备
^!h::
Run, powershell.exe -Command "Get-AudioDevice"
return
获取设备ID的方法
Windows (PowerShell)
# 先安装模块 Install-Module -Name AudioDeviceCmdlets # 列出所有设备 Get-AudioDevice -List # 获取当前设备ID Get-AudioDevice | Select-Object ID
Linux
# 列出音频设备
pactl list short sinks | awk '{print $2}'
macOS
# 列出音频设备 SwitchAudioSource -a
创建桌面快捷方式
Windows (.bat文件)
@echo off
:: speakers-preset.bat
powershell -Command "Set-AudioDevice -ID '{音箱设备ID}'"
Linux (.desktop文件)
[Desktop Entry] Name=切换到耳机 Exec=/home/user/scripts/audio-preset.sh headphones Type=Application Icon=audio-headphones
推荐使用方案
- Windows用户: 使用 AutoHotkey 绑定快捷键
- Linux用户: 使用 bash脚本 + 自定义快捷键
- macOS用户: 使用 SwitchAudioSource + Alfred/Keyboard Maestro
需要我帮您生成特定系统的完整配置脚本吗?