【问题标题】:How to change between speakers and USB headset using a script on Ubuntu?如何在 Ubuntu 上使用脚本在扬声器和 USB 耳机之间进行切换?
【发布时间】:2016-06-16 05:42:34
【问题描述】:

我正在尝试编写一个脚本来在扬声器和耳机之间进行切换。我希望能够使用快捷方式更改输出和输入。

我使用这些命令查看我的音频设备:

$ pacmd list-sinks | grep alsa_output 

name: <alsa_output.usb-Logitech_Logitech_USB_Headset-00.analog-stereo>
name: <alsa_output.pci-0000_00_1b.0.analog-stereo>

$ pacmd list-sources | grep alsa_input

name: <alsa_input.usb-Logitech_Logitech_USB_Headset-00.analog-mono>
name: <alsa_input.usb-093a_262c-01.analog-mono>
name: <alsa_input.pci-0000_00_1b.0.analog-stereo>

并编写了这个脚本,以便我可以更改默认音频设备。

#! /bin/bash

  pacmd set-default-sink alsa_output.usb-Logitech_Logitech_USB_Headset-00.analog-stereo
  pacmd set-default-source alsa_input.usb-Logitech_Logitech_USB_Headset-00.analog-mono

exit 0

当我用命令运行它时

 sudo ./usb-speakers.sh 

我收到这条消息:

没有 PulseAudio 守护程序在运行,或者没有作为会话守护程序运行。
主目录无法访问:权限被拒绝
没有 PulseAudio 守护程序正在运行,或者没有作为会话守护程序运行。

我发现了几个类似问题的问题,但我无法为我做任何事情。

【问题讨论】:

    标签: bash ubuntu audio alsa


    【解决方案1】:

    https://github.com/mk-fg/pulseaudio-mixer-cli/blob/master/README.md 说:

    有点像 alsamixer,重点不是接收器的音量级别(实际上可以通过 alsamixer 使用 alsa-pulse 插件进行控制),而是关注各个流的音量,因此您可以调低音乐以聆听游戏中的内容、mumble、Skype 或浏览器。

    除了交互式 UI,脚本还允许通过配置文件匹配和配置接收器/流参数,以便在出现特定接收器或流时,例如它的音量可以设置上限、端口更改、UI 标题调整、隐藏在 UI 中,诸如此类。

    【讨论】:

    • 您好,感谢您的回答我尝试安装此插件,但出现以下错误:File "./pa-mixer-mk3.py", line 10, in &lt;module&gt; from pulsectl import Pulse, PulseLoopStop, PulseDisconnected, PulseIndexError ImportError: No module named 'pulsectl' 我使用此命令安装了pulsectlapt-get install python-pip pip install pulsectl
    • 对不起,我不知道为什么它不起作用。当我面前有一个 Ubuntu 盒子而不是手机时,我会尝试复制。
    【解决方案2】:

    我找到了解决我所有问题的脚本(我不记得在哪里)

    #!/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;
    

    只需将其复制并粘贴到一个空文档中,然后另存为 audio-device-swithcer.sh

    然后将其保存到您的usr/local/bin 目录

    如果您想添加快捷方式,请转到 Keyboard &amp; Shortcuts 并添加新快捷方式。 作为命令,只需输入您的 audio-device-switcher.sh 文件

    这就是所有为我工作的人。 并感谢您的回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多