【问题标题】:How to programmatically change volume in Ubuntu如何以编程方式更改 Ubuntu 中的音量
【发布时间】:2012-05-31 03:34:57
【问题描述】:

如何在 Ubuntu 上以编程方式更改 Gnome 中的音量,无论是从命令行还是 API(最好是 Python)?

我找到的类似问题的唯一答案使用amixer,这似乎对 Ubuntu 12.04 没有影响。运行:

amixer set Headphone 10-

显示:

Simple mixer control 'Headphone',0
  Capabilities: pvolume pswitch penum
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 115
  Mono:
  Front Left: Playback 0 [57%] [-57.50dB] [on]
  Front Right: Playback 0 [57%] [-57.50dB] [on]

每次运行时 x% 都会发生变化。不幸的是,它对实际音量没有影响。最终它显示为 0%,但音量仍处于最高水平。

另一个缺点是我必须指定确切的活动输出设备,如果有多个设备我可能不知道。例如,如果我有一个“Master”和“Headphone”,我如何确定哪一个是活动设备?

【问题讨论】:

  • 只是猜测:amixer set Master 10- 有效吗?更改主音量会影响所有其他通道。据我所知。
  • @aland: amixer set Master 10- 工作。
  • amixer 没有任何效果,无论我指定哪个设备...

标签: python audio ubuntu gnome alsa


【解决方案1】:

Ubuntu 使用 pulseaudio 作为发声器。可以使用pactlpacmd 实用程序从命令行控制它,例如:

pactl set-sink-volume 0 20%

将 #0 水槽的体积设置为 20%。

请参阅:man pactlpacmd help


编辑:

为避免-xx 被解释为命令行选项,您必须在其前面加上--。从那时起停止选项解析:

pactl set-sink-volume 0 -- -20%    # or:
pactl -- set-sink-volume 0 -20%    # doesn't matter where the `--` goes

【讨论】:

  • 如何设置相对音量变化?手册页说“如果音量规格以 + 或 - 开头,音量调整将相对于当前接收器音量。”但是做pactl set-sink-volume 0 -10% 会给我错误“pactl: invalid option -- '1'”
  • @Cerin - 这是 shell 中选项解析的常见问题...更新了我的答案。
  • 感谢您的澄清。
  • 对 Ubuntu 12 没有影响。
  • 这在 Ubuntu 13.04 中对我有用(也发布为下面的答案,请为可见性投票)。 amixer -D pulse sset Master 5%+
【解决方案2】:

我使用 ALSA 混音器来做。你可能需要下载 python-alsaaudio

sudo apt-get install python-alsaaudio

然后控制音量,

import alsaaudio
m = alsaaudio.Mixer()   # defined alsaaudio.Mixer to change volume
m.setvolume(50) # set volume
vol = m.getvolume() # get volume float value

阅读http://pyalsaaudio.sourceforge.net/libalsaaudio.html 了解有关 alsaaudio 库的详细信息。

【讨论】:

  • 我知道,但我使用的是 ubuntu 12.04,这仍然有效。所以,我想你可以使用它。
  • 很奇怪。我也在 Macbook 上使用 12.04,而且没有一个 alsa 实用程序对我有用。但是,所有的 pulseaudio 实用程序都可以完美运行...
  • Pulseaudio 永远不会直接与硬件对话,它仍然会在相当长的时间内使用 ALSA。而且,并不是每个人都在使用或打算使用 pulseaudio。
  • @dom0 不过,这对我来说仍然不起作用。设置音量没有任何作用。
  • 如果您使用 Pulseaudio,您需要明确指定卡 ID(通常为 0),因为您希望使用 alsamixer [将 cardindex=0 传递给 alsaaudio.Mixer]。如果不这样做,您将通过一些包装库自动连接到 pulseaudio(您将从 PA 获得的模拟 ALSA 混音器无法更改系统音量)。我在使用 Pulseaudio 时成功地使用它来读取和更改音量:github.com/enkore/i3pystatus/blob/master/i3pystatus/alsa.py#L41
【解决方案3】:

amixer 命令在 Ubuntu 13.04 中工作,

音量增加 5%
amixer -D pulse sset Master 5%+

音量减少 5%
amixer -D pulse sset Master 5%-

pactlpacmd 在 Ubuntu 13.04 中无法正常工作。

【讨论】:

  • 建议的amixer set Master 10- 和你的命令都对我有用,谢谢!
【解决方案4】:

读取音量的脏 sn-p(不要忘记音量在 ubuntu 上超过“100%” - 此时返回 ~0.66)。

#!/usr/bin/python
import subprocess

vol = int(filter(lambda l: l.startswith('set-sink-volume'),
          subprocess.check_output(["pacmd","dump"])
          .split('\n'))[0]
          .split()[-1],16)/100000.

print vol

【讨论】:

    【解决方案5】:

    你也可以试试简洁优雅的ponymix utill。它使增加/减少音量、切换(静音/取消静音)音频等变得非常容易。

    首先使用ponymix获取可用音频源列表

    就我而言,我可以同时看到 sink 0source 0。我可以使用数字 0 或全名 Built-in Audio Digital Stereo (HDMI) 来控制音频。

    增加卡0的音量5%ponymix -c 0 increase 5

    将卡0的音量降低5%ponymix -c 0 decrease 5

    【讨论】:

      【解决方案6】:

      我可以推荐这个控制pulseaudio的工具: https://github.com/graysky2/pulseaudio-ctl

      me@mypc ~ $ pulseaudio-ctl
      pulseaudio-ctl v1.63
      
      /usr/bin/pulseaudio-ctl {up,down,mute,mute-input,set,atmost,full-status} [n]
      
      Where up and down adjust volume in ±5 % increments
      Where up and down [n] adjust volume in ±n % increments
      Where mute toggles the mute status on/off
      Where mute-input toggles the input status on/off
      Where set set the volume to [n] %
      Where atmost only takes effect if current volume is higher than [n]
      Where full-status prints volume level, sink and source mute state to stdout
      
      Optionally, redefine an upper threshold in /home/me/.config/pulseaudio-ctl/config
      
      Volume level     : 80 %
      Is sink muted    : no
      Is source muted  : no
      Detected sink    : 1
      Detected source  : 3
      Pulse version    : 8.0
      me@mypc ~ $ 
      

      【讨论】:

        猜你喜欢
        • 2013-01-24
        • 1970-01-01
        • 2023-04-11
        • 2023-03-14
        • 2015-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-16
        相关资源
        最近更新 更多