在两个声音设备之间切换的脚本

在两个声音设备之间切换的脚本

我使用两个音频设备,普通模拟输出和 HDMI 声音。当我不使用电视时,我会将其关闭并切换到扬声器。由于我经常这样做,所以我希望有一个脚本可以帮我完成这件事。

我确实有一个可以在两者之间切换的脚本,但仅适用于当前产生声音的应用程序。我把它弄丢了,似乎找不到互联网来源。

有人能帮我重新创建这个脚本吗?

答案1

我终于找到了脚本。此脚本会将所有输入接收器(即您的音乐、视频声音等)移至您的其他声音设备。如果您再次运行该脚本,它会将所有接收器移回您的原始声音设备。(我只在两个声音设备上测试过)。

设置说明:

  1. 使用 gedit 将以下代码粘贴到文件中。
  2. 将文件保存为 Speakers.sh(或其他文件名)到您的主目录中。
  3. 更改文件权限以允许执行。

    • 右键单击该文件并选择属性。
    • 选择权限选项卡。
    • 勾选“允许将文件作为程序执行”复选框

    或者

    • chmod +x ./speakers.sh
  4. 双击该文件并单击运行。它将更改输出声音设备。

然后,我在 Unity 启动器的设置图标上添加了一个选项,以便于使用Ubuntu 调整。 享受!

代码:

#!/bin/bash

declare -i sinks=(`pacmd list-sinks | sed -n -e 's/\**[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`)
declare -i sinks_count=${#sinks[*]}
declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
declare -i next_sink_index=${sinks[0]}

#find the next sink (not always the next index number)
declare -i ord=0
while [ $ord -lt $sinks_count ];
do
echo ${sinks[$ord]}
if [ ${sinks[$ord]} -gt $active_sink_index ] ; then
    next_sink_index=${sinks[$ord]}
    break
fi
let ord++
done

#change the default sink
pacmd "set-default-sink ${next_sink_index}"

#move all inputs to the new sink
for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
do
pacmd "move-sink-input $app $next_sink_index"
done

#display notification
declare -i ndx=0
pacmd list-sinks | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line;
do
if [ $(( $ord % $sinks_count )) -eq $ndx ] ; then
    notify-send -i notification-audio-volume-high --hint=string:x-canonical-private-synchronous: "Sound output switched to" "$line"
    exit
fi
let ndx++
done;

所有的功劳都归于这些家伙这里,我刚刚重新找到了我的问题的答案。

相关内容