本文目录导读:

这是一个自动切换音频输出设备并发送通知的脚本,我为您提供 Windows 和 macOS 两种版本。
Windows 版本 (PowerShell)
基本版 - 切换到下一个音频设备
# AudioOutputSwitcher.ps1
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class AudioSwitcher {
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
"@
# 获取当前默认音频输出设备
$RegPath = "HKCU:\Software\Microsoft\Multimedia\Audio"
$CurrentDevice = (Get-ItemProperty -Path $RegPath -Name "DefaultDevice" -ErrorAction SilentlyContinue).DefaultDevice
# 获取所有音频设备
$Devices = Get-AudioDevice -List | Where-Object { $_.Type -eq "Playback" }
$DeviceNames = $Devices | ForEach-Object { $_.Name }
if ($DeviceNames.Count -eq 0) {
Write-Host "没有找到音频输出设备" -ForegroundColor Red
exit
}
# 找到当前设备索引
$CurrentIndex = -1
for ($i = 0; $i -lt $Devices.Count; $i++) {
if ($Devices[$i].ID -eq $CurrentDevice) {
$CurrentIndex = $i
break
}
}
# 切换到下一个设备
if ($CurrentIndex -eq -1) {
$NextIndex = 0
} else {
$NextIndex = ($CurrentIndex + 1) % $Devices.Count
}
$NextDevice = $Devices[$NextIndex]
# 设置新的默认设备
Set-AudioDevice -ID $NextDevice.ID
# 发送通知
$Notification = New-Object System.Windows.Forms.NotifyIcon
$Notification.Icon = [System.Drawing.SystemIcons]::Information
$Notification.Visible = $true
$Notification.ShowBalloonTip(3000, "音频设备切换", "已切换到: $($NextDevice.Name)", [System.Windows.Forms.ToolTipIcon]::Info)
Write-Host "音频设备已切换到: $($NextDevice.Name)" -ForegroundColor Green
# 清理
$Notification.Dispose()
增强版 - 带系统托盘图标
# AudioSwitcherTray.ps1 (需要保存为脚本并运行)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$trayIcon = New-Object System.Windows.Forms.NotifyIcon
$trayIcon.Icon = [System.Drawing.SystemIcons]::Information
$trayIcon.Visible = $true
$contextMenu = New-Object System.Windows.Forms.ContextMenuStrip
$switchItem = New-Object System.Windows.Forms.ToolStripMenuItem("切换音频设备")
$switchItem.Add_Click({
# 调用切换逻辑
& ".\AudioOutputSwitcher.ps1"
})
$exitItem = New-Object System.Windows.Forms.ToolStripMenuItem("退出")
$exitItem.Add_Click({
$trayIcon.Visible = $false
[System.Windows.Forms.Application]::Exit()
})
$contextMenu.Items.Add($switchItem)
$contextMenu.Items.Add($exitItem)
$trayIcon.ContextMenuStrip = $contextMenu
$trayIcon.Text = "音频设备切换器"
$trayIcon.ShowBalloonTip(1000, "音频设备切换器", "已启动,右键点击托盘图标操作", [System.Windows.Forms.ToolTipIcon]::Info)
[System.Windows.Forms.Application]::Run()
macOS 版本 (Shell Script)
#!/bin/bash
# AudioOutputSwitcher.sh
# 获取所有音频输出设备
get_devices() {
system_profiler SPAudioDataType | grep -A 12 "Output:" | grep "Audio Device" | sed 's/.*: //' | sort -u
}
# 获取当前默认输出设备
get_current_device() {
SwitchAudioSource -t output -c
}
# 切换音频设备
switch_audio() {
local devices=($(get_devices))
local current=$(get_current_device)
local count=${#devices[@]}
if [ $count -eq 0 ]; then
osascript -e 'display notification "没有找到音频输出设备" with title "错误"'
exit 1
fi
# 找到当前设备索引
local current_index=-1
for i in "${!devices[@]}"; do
if [ "${devices[$i]}" == "$current" ]; then
current_index=$i
break
fi
done
# 切换到下一个设备
local next_index=$(( (current_index + 1) % count ))
local next_device="${devices[$next_index]}"
# 执行切换
SwitchAudioSource -s "$next_device"
# 发送通知
osascript -e "display notification \"已切换到: $next_device\" with title \"音频设备切换\""
echo "已切换到: $next_device"
}
# 如果直接运行,执行切换
if [ "$1" != "--daemon" ]; then
switch_audio
fi
# 后台运行模式
if [ "$1" == "--daemon" ]; then
while true; do
# 监听快捷键 (Ctrl+Alt+A)
# 这里需要配置快捷键绑定,例如使用 Hammerspoon 或 Keyboard Maestro
sleep 1
done
fi
macOS 自动执行版本 (使用 launchd)
#!/bin/bash
# auto_switch_audio.sh
# 自动切换逻辑
CURRENT_DEVICE=$(SwitchAudioSource -t output -c)
DEVICES=($(SwitchAudioSource -t output -a))
DEVICE_COUNT=${#DEVICES[@]}
for i in "${!DEVICES[@]}"; do
if [ "${DEVICES[$i]}" == "$CURRENT_DEVICE" ]; then
NEXT_INDEX=$(( (i + 1) % DEVICE_COUNT ))
NEXT_DEVICE="${DEVICES[$NEXT_INDEX]}"
SwitchAudioSource -s "$NEXT_DEVICE"
osascript -e "display notification \"已切换到: $NEXT_DEVICE\" with title \"音频设备切换\""
exit 0
fi
done
安装和使用说明
Windows 使用
-
安装 PowerShell 音频模块:
Install-Module -Name AudioDeviceCmdlets -Force
-
运行脚本:
powershell -ExecutionPolicy Bypass -File AudioOutputSwitcher.ps1
-
设置快捷键:
- 创建快捷方式到脚本
- 在快捷方式属性中设置快捷键
macOS 使用
-
安装 SwitchAudioSource:
brew install switchaudio-osx
-
赋予执行权限:
chmod +x AudioOutputSwitcher.sh
-
绑定快捷键(使用 Hammerspoon):
-- ~/.hammerspoon/init.lua hs.hotkey.bind({"ctrl", "alt"}, "A", function() hs.execute("/path/to/AudioOutputSwitcher.sh") end)
注意事项
- 权限要求: Windows 版本需要管理员权限运行
- 通知显示: 确保系统通知功能已开启
- 音频设备识别: 部分虚拟音频设备可能不会被正确识别
您可以根据需要调整脚本,例如添加特定的设备优先顺序或自定义通知样式,需要我帮您定制特定功能吗?