gpt4 book ai didi

php - 使用 php exec 和 amixer 设置音量

转载 作者:IT王子 更新时间:2023-10-29 00:05:57 28 4
gpt4 key购买 nike

我写了一个小的 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]

我正在播放音乐,但声音越来越大。

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

# 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

在命令行上只有 default 和 pulse 工作:

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

对于 PHP,即使这两个也不起作用。无论如何......我的声音来 self 的显示器扬声器,它是通过 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

更新

aplay -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 没有寻址到正确的 DBUS当从 apache 运行时。尝试通过在运行 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

(代码复制自 Running command-line application from PHP as specific user )

amixer.php

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

关于php - 使用 php exec 和 amixer 设置音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36867273/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com