pacmd - 播放期间将输入移动到不同接收器的 udev 规则

pacmd - 播放期间将输入移动到不同接收器的 udev 规则

我是新手。

我希望能够在播放期间通过插入 HDMI 电缆切换到 HDMI 音频输出。我遵循了此处给出的建议:如何在播放过程中使用“pacmd set-default-sink”更改 pulseaudio 接收器?

我已将其集成到 udev 规则中,但它仍然不会自动移动输入流。如果我从终端运行相同的规则/脚本,它就会起作用。您可以在下面找到脚本。

注意:每次更改脚本时,我都会运行“sudo udevadm control --reload-rules”。

我也无法从 pactl 或 xrandr 检索 hdmi 视频输出名称和音频接收器编号,所以我手动编写了它们,我应该阅读一些 bash 脚本。如果有人能帮助我,我会很高兴。如果我使用了错误的术语,请原谅。提前致谢。

#!/bin/bash
# Sound Toggle
# By Charles Cruz

########### Settings ###########
# Backlight Settings
BACKLIGHT_BATTERY=20
BACKLIGHT_AC=100

USERID="$(cat /var/run/ConsoleKit/database | grep -B 6 is_active=true | grep uid= | cut -f 2 -d '=')"
USER="$(grep $USERID /etc/passwd | cut -f 1 -d ':')"
HDMI_STATUS="$(cat /sys/class/drm/card0-HDMI-A-1/status)"
GNOME_SCREENSAVER_PROC=`ps xa | grep gnome-screensaver | head -n 1 | awk '{print $1}'`

INT="eDP1"
EXT="HDMI1"

export `grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SCREENSAVER_PROC/environ`
export XAUTHORITY="/home/$USER/.Xauthority"
export DISPLAY="$(cat /var/run/ConsoleKit/database | grep x11_display= | cut -f 2 -d '=')"

# HDMI is connected
if [ "$HDMI_STATUS" = connected ]; then

# Set output to HDMI
sudo -u $USER xrandr --output $EXT --auto
sudo -u $USER xrandr --output $INT --off

# Set the sound card profile
 sudo -u $USER pactl set-default-sink 0

# Move inputs to HDMI

inputs=$(pacmd list-sink-inputs | awk '$1 == "index:" {print $2}')
for INPUT in $inputs; do # Move all current inputs to the new default sound card
 pacmd move-sink-input $INPUT 0
done

# Disable the lockscreen if it's enabled so you can close the lockscreen
if [[ $(sudo -u $USER gsettings get org.gnome.desktop.screensaver lock-enabled) = true ]]; then
sudo -u $USER gsettings set org.gnome.desktop.screensaver lock-enabled false
fi

# HDMI is not connected
else

# Set output to INTERNAL
sudo -u $USER xrandr --output $INT --auto
sudo -u $USER xrandr --output $EXT --off 

# Set the sound card profile
 sudo -u $USER pactl set-default-sink-1

# Move inputs to internal

inputs=$(pacmd list-sink-inputs | awk '$1 == "index:" {print $2}')
for INPUT in $inputs; do # Move all current inputs to the new default sound card
  pacmd move-sink-input $INPUT 1
done

# Restore the lock setting, if necessary
if [[ $(sudo -u $USER gsettings get org.gnome.desktop.screensaver lock-enabled) = false ]]; then
sudo -u $USER gsettings set org.gnome.desktop.screensaver lock-enabled true
fi

# Restore default battery/power brightness
cat /proc/acpi/ac_adapter/AC/state | grep "on-line"
if [ $? -eq 0 ]; then
xbacklight -set $BACKLIGHT_AC # Power cable is connected
else
xbacklight -set $BACKLIGHT_BATTERY # Power cable is not connected
fi
fi

exit 0

这是 udev 规则。它的路径是 /etc/udev/rules.d/hdmi.rules 上一个脚本的路径是 /usr/bin/toggle-sound

SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/bin/toggle-sound"

相关内容