sox 失败格式:无法打开输入“alsa_output.pci-0000_00_1b.0.analog-stereo.monitor”:无法打开音频设备:连接被拒绝

sox 失败格式:无法打开输入“alsa_output.pci-0000_00_1b.0.analog-stereo.monitor”:无法打开音频设备:连接被拒绝

我编写了以下服务来监听音频输入:

[Unit]
Description=Monitor Audio Output
After=syslog.target network.target plusaudio.service

[Service]
Type=simple
ExecStart=sox -v 5 -t pulseaudio alsa_output.pci-0000_00_1b.0.analog-stereo.monitor  -t mp3 /home/ahmad/recordings/k2.mp3 silence 1 0.1 3% 1 3.0 3%

Restart=on-success
EnvironmentFile=/etc/environment
User=ahmad

[Install]
WantedBy=default.target

如果我sox在终端中运行命令,一切都很好并且它正在收听音频。但是,如果我启动该服务并检查其状态,它会显示:

    ● runsox.service - Monitor Audio Output
     Loaded: loaded (/etc/systemd/system/runsox.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2020-07-23 21:06:28 +0430; 43s ago
    Process: 5517 ExecStart=/usr/bin/sox -v 5 -t pulseaudio alsa_output.pci-0000_00_1b.0.analog-stereo.monitor -t mp3 /home/ahmad/recordings/k2.mp3 silence 1 0.1 3% 1 3.0 3% (code=exited, status=2)
   Main PID: 5517 (code=exited, status=2)

جولای 23 21:06:27 app systemd[1]: Started Monitor Audio Output.
جولای 23 21:06:28 app sox[5517]: /usr/bin/sox FAIL formats: can't open input  `alsa_output.pci-0000_00_1b.0.analog-stereo.monitor': can not open audio device: Connection refused
جولای 23 21:06:28 app systemd[1]: runsox.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
جولای 23 21:06:28 app systemd[1]: runsox.service: Failed with result 'exit-code'.

更新

我注意到,如果我在终端中运行命令sudo,我会得到同样的错误,所以权限或脉冲设置一定有问题。

sudo /usr/bin/sox -v 5 -t pulseaudio alsa_output.pci-0000_00_1b.0.analog-stereo.monitor -t mp3 /home/ahmad/recordings/k2.mp3 silence 1 0.1 3% 1 3.0 3%
/usr/bin/sox FAIL formats: can't open input  `alsa_output.pci-0000_00_1b.0.analog-stereo.monitor': can not open audio device: Connection refused

答案1

问题是pulseaudio 不是在系统范围内运行,因此pulseaudio 服务不会在启动时运行。

不建议在桌面系统范围内运行它。但是,如果您确实想这样做,请参阅:

Pulseaudio 作为系统范围的 systemd 服务

相关内容