使用 php exec 和 amixer 设置音量


Set volume using php exec and amixer

我写了一个小的php脚本来用alsa控制本地机器的音量:

<?php
# for simplicity and testing it really just executes the command:
echo exec('amixer set Master 5%+') . " 'n";

现在,当我在命令行上运行此脚本时,它工作正常:

$ php volume.php 
Front Right: Playback 39226 [60%] [on] 
$ php volume.php 
Front Right: Playback 42503 [65%] [on] 
$ php volume.php 
Front Right: Playback 45780 [70%] [on]

正在播放音乐,我听到它的声音越来越大。

但是当我尝试从调用http://localhost/volume.php的浏览器通过 apache 运行脚本时,它不起作用。

# http://localhost/volume.php
Front Right: Playback 55709 [10%] [on]
# F5
Front Right: Playback 55709 [15%] [on]
# F5
Front Right: Playback 55709 [20%] [on]

现在我听到音量没有变化,百分比似乎与当前状态无关。它说10% - 15% - 20%,而实际上它仍然是70%。

我的

apache 以我的用户身份运行exec('whoami')因此它给了我在 shell 上登录的用户名,一切都运行良好。

# httpd.conf
User mkt
Group mkt

我在 Fedora 22 上。

可能是由于 apache2 进程环境。有什么想法可以解决这个问题吗?

更新:

这是 aplay -L 的输出:

[mkt@localhost ~]$ aplay -L
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
default
    Default ALSA Output (currently PulseAudio Sound Server)
sysdefault:CARD=Intel
    HDA Intel, ALC888 Analog
    Default Audio Device
front:CARD=Intel,DEV=0
    HDA Intel, ALC888 Analog
    Front speakers
surround21:CARD=Intel,DEV=0
    HDA Intel, ALC888 Analog
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Intel,DEV=0
    HDA Intel, ALC888 Analog
    4.0 Surround output to Front and Rear speakers
surround41:CARD=Intel,DEV=0
    HDA Intel, ALC888 Analog
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Intel,DEV=0
    HDA Intel, ALC888 Analog
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Intel,DEV=0
    HDA Intel, ALC888 Analog
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Intel,DEV=0
    HDA Intel, ALC888 Analog
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Intel,DEV=0
    HDA Intel, ALC888 Digital
    IEC958 (S/PDIF) Digital Audio Output
hdmi:CARD=NVidia,DEV=0
    HDA NVidia, HDMI 0
    HDMI Audio Output
hdmi:CARD=NVidia,DEV=1
    HDA NVidia, HDMI 1
    HDMI Audio Output
hdmi:CARD=NVidia,DEV=2
    HDA NVidia, HDMI 2
    HDMI Audio Output
hdmi:CARD=NVidia,DEV=3
    HDA NVidia, HDMI 3
    HDMI Audio Output

在命令行上,只有默认值和脉冲起作用:

amixer -D pulse set Master 5%+
amixer -D default set Master 5%+

使用PHP,即使这两个也不起作用。无论如何。。。我的声音来自通过HDMI插入的显示器扬声器。所以我想最后 4 台设备是我的候选人。但是他们都没有工作。

$ amixer -D hdmi:CARD=NVidia,DEV=0 set Master 5%+
$ amixer -D hdmi:CARD=NVidia,DEV=1 set Master 5%+
$ amixer -D hdmi:CARD=NVidia,DEV=2 set Master 5%+
$ amixer -D hdmi:CARD=NVidia,DEV=3 set Master 5%+

在所有四种情况下,它都说:(当然是 DEV=[0-3])

ALSA lib control.c:954:(snd_ctl_open_noupdate) Invalid CTL hdmi:CARD=NVidia,DEV=3
amixer: Mixer attach hdmi:CARD=NVidia,DEV=3 error: No such file or directory

更新

输出 -l:

$ aplay -l
**** List of Hardware-Devices (PLAYBACK) ****
Card 0: Intel [HDA Intel], Device 0: ALC888 Analog [ALC888 Analog]
  Sub-Devices: 1/1
  Sub-Device #0: subdevice #0
Card 0: Intel [HDA Intel], Device 1: ALC888 Digital [ALC888 Digital]
  Sub-Devices: 1/1
  Sub-Device #0: subdevice #0
Card 1: NVidia [HDA NVidia], Device 3: HDMI 0 [HDMI 0]
  Sub-Devices: 1/1
  Sub-Device #0: subdevice #0
Card 1: NVidia [HDA NVidia], Device 7: HDMI 1 [HDMI 1]
  Sub-Devices: 0/1
  Sub-Device #0: subdevice #0
Card 1: NVidia [HDA NVidia], Device 8: HDMI 2 [HDMI 2]
  Sub-Devices: 1/1
  Sub-Device #0: subdevice #0
Card 1: NVidia [HDA NVidia], Device 9: HDMI 3 [HDMI 3]
  Sub-Devices: 1/1
  Sub-Device #0: subdevice #0
$ amixer -c0 set Master 5%+
$ amixer -c1 set Master 5%+

两者都不行!

溶液:

感谢所有的帮助!答案虽然来自 https://superuser.com/questions/1069981/set-volume-using-php-exec-and-amixer

putenv("PULSE_SERVER=/run/user/".getmyuid()."/pulse/native");

也许amixer从 apache 运行时没有解决正确的 DBUS。尝试通过在运行 amixer 之前从设置变量的 shell 脚本调用amixer来设置DBUS_ADDRESS环境变量。

dbus_amixer.sh

#! /bin/bash
DBUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS /proc/*/environ 2> /dev/null| sed 's/DBUS/'nDBUS/g' | tail -n 1`
if [ "x$DBUS_ADDRESS" != "x" ]; then
        export $DBUS_ADDRESS
        /usr/bin/amixer set Master 5%+
fi

(以特定用户身份从 PHP 运行命令行应用程序复制的代码)

混音器.php

<?php
echo exec('dbus_amixer.sh') . " 'n";

尝试先运行aplay -L,你应该得到这样的输出:

pulse
    PulseAudio Sound Server
sysdefault:CARD=MID
    HDA Intel MID, ALC889 Analog
    Default Audio Device
front:CARD=MID,DEV=0
    HDA Intel MID, ALC889 Analog
    Front speakers
surround21:CARD=MID,DEV=0
    HDA Intel MID, ALC889 Analog
    2.1 Surround output to Front and Subwoofer speakers
...

确定其中哪个是您的设备,然后修改您的amixer ...命令以amixer -D device ...,例如amixer -D surround21:CARD=MID,DEV=0 set Master 5%+

这可能会起作用。 如果没有,请尝试aplay -l(小写),并获取卡号。 然后,例如,如果卡号为 1 ,请尝试 amixer -c 1 set Master 5%+

你说你的 Apache 用户以你的命令行用户身份运行。相似性延伸多远?我注意到您使用短路径:

echo exec('amixer set Master 5%+')

。鉴于用户是相同的,您可能在 Apache 中有错误的路径(PATH 设置不依赖于用户,它们在用户配置文件中,Apache 可能会完全加载不同的配置文件) - 尝试将要amixer的完整路径放入 exec:

echo exec('/usr/local/bin/or/whatever/amixer set Master 5%+') . " 'n";

此外,出于调试目的,运行shell_exec 2>&1 stderr 重定向到命令行。这可能会产生一些关于究竟是什么失败的线索。

您可以运行更复杂的命令来设置环境:

exec(`HOME=whatever FLAGS=somethingelse PATH=/usr/local/... /usr/local/bin/amixer... 2>&1`);

要了解环境变量是什么,您可以以用户身份登录,验证 amixer 是否正常工作,将环境转储到文件中:

设置>集合.txt

并查看 set.txt 以获取可应用于混音器的任何值。您可以从 Apache 中运行另一个类似的set,并比较结果(实际上我认为环境也可以通过 phpinfo() 访问)。

完成

此操作的最佳和更简单的方法是使用 xdotool。

<?php
shell_exec("DISPLAY=:0 xdotool key XF86AudioMute");
?>
<?php
shell_exec("DISPLAY=:0 xdotool key XF86AudioRaiseVolume");
?>
<?php
shell_exec("DISPLAY=:0 xdotool key XF86AudioLowerVolume");
?>

我安装了 xdotool 并在 ubuntu openbox 上配置了 rc.xml 文件

 <!-- Keybinding for Volume management -->
   <keybind key="XF86AudioRaiseVolume">
     <action name="Execute">
       <command>amixer -D pulse set Master 10%+</command>
     </action>
  </keybind>
   <keybind key="XF86AudioLowerVolume">
     <action name="Execute">
       <command>amixer -D pulse set Master 10%-</command>
     </action>
   </keybind>
   <keybind key="XF86AudioMute">
     <action name="Execute">
       <command>amixer -D pulse set Master 100%-</command>
     </action>
  </keybind>

它就像一个魅力。