如何使用快捷键切换声音输出

如何使用快捷键切换声音输出

我喜欢使用快捷方式将声音输出从扬声器切换到 USB 耳机。有什么方法可以实现吗?

答案1

  1. 检查端口名称pactl list sinks(我删除了不需要的接收器输出):

    Sink #1
        State: RUNNING
        Name: alsa_output.pci-0000_00_1b.0.analog-stereo
        Description: Built-in Audio Analog Stereo
        Driver: module-alsa-card.c
    ...
        Ports:
            analog-output-speaker: Speakers (priority: 10000, not available)
            analog-output-headphones: Headphones (priority: 9000, available)
        Active Port: analog-output-headphones
        Formats:
            pcm
    
  2. 使用以下方式设置接收端口pactl set-sink-port

     pactl set-sink-port 1 analog-output-speaker
    

    或者

     pactl set-sink-port 1 analog-output-headphones
    

    如果你正在使用可移动设备(例如:USB 设备),最好使用 sinkname而不是id。例如:

     pactl set-sink-port alsa_output.pci-0000_00_1b.0.analog-stereo analog-output-headphones
    

参考: man pactl

答案2

自动化解决方案https://ubuntuforums.org/showthread.php?t=1370383 它适用于 Ubuntu 18.04

  1. 打开终端并输入:

     sudoedit /usr/local/bin/audio-device-switch.sh
    
  2. 复制并粘贴以下代码到 nano 编辑器中

  3. 保存并关闭 nano 编辑器。

  4. sudo chmod 755 /usr/local/bin/audio-device-switch.sh

  5. 系统->优先->键盘快捷键

  6. 添加并输入在音频设备之间切换作为名称和 audio-device-switch.sh 作为命令并按申请

  7. 选择新添加的快捷方式行并点击捷径列。8. 选择快捷方式组合 – 例如Win+ F12

  8. 就这样 - 现在您可以插入 HDMI 设备并通过按下所选的快捷组合来切换音频输出。

代码:

#!/bin/bash

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

if [ $active_sink_index -ne $major_sink_index ] ; then
    next_sink_index=active_sink_index+1
fi

#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 [ $next_sink_index -eq $ndx ] ; then
        notify-send -i notification-audio-volume-high "Sound output switched to" "$line"
        exit
    fi
done

答案3

由于每个人都添加了他们的解决方案,所以这是我的。

#!/bin/sh

currentline=$(pactl list short sinks | grep -n "$(pactl get-default-sink)" | cut -d: -f 1)
lastline=$(pactl list short sinks | wc -l)
nextline=$(($currentline % $lastline + 1))
nextsink=$(pactl list short sinks | head "-n$nextline" | tail -1 | cut -f 1)

pactl set-default-sink $nextsink

for sinkinput in $(pactl list short sink-inputs | cut -f 1); do
  pactl move-sink-input $sinkinput "@DEFAULT_SINK@"
done

答案4

它不适用于两位数索引。在我的例子中,Nvidia HDMI 接收器的索引为 23。这是一个可行的解决方案 :)

#!/bin/bash    
    
declare -i sinks_count=`pacmd list-sinks | grep -Pc 'index:\s+\d+'`    
    
if [ $sinks_count -eq 0 ] ; then    
    exit    
fi    
    
declare -i active_sink_index=`pacmd list-sinks | grep -Po '\*\s+index:\s+\K\d+'`    
    
active_index_position_found=0    
let next_sink_index=-1    
while read index ;    
do    
    declare -i ind=($(echo $index | tr -dc '[0-9]+'))    
    if [ $next_sink_index -lt 0 ] ; then    
        export next_sink_index=$ind    
    fi    
    if [ $active_index_position_found -eq 1 ] ; then    
        export next_sink_index=$ind    
        break;    
    fi    
    if [ $active_sink_index -eq $ind ] ; then    
        export active_index_position_found=1    
    fi    
done < <(pacmd list-sinks | grep -Po 'index:\s+\K\d+')    
    
#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 | grep -Po 'index:\s+\K\d+');
do
    pacmd "move-sink-input $app $next_sink_index"
done

相关内容