从守护进程Linux播放声音

我有Manjaro Linux,并且尝试使用守护程序中的脚本播放声音(mp3和wav)。 基本上,我有一个script.sh,其中包含尝试播放声音的命令。此脚本是从守护程序调用的。 (下面的代码的更多详细信息)。

快速说明:

  • The script run successfully from my command line (Without daemon)
  • I know my script use root as user, but I need the sound in my session, for that, I just add a "prefix" before the main command to execute the command as user myself
  • As a single command for play sound (Without daemon and without prefix) my both options works (I tried with paplay and cvlc)
  • With the "Prefix" only works cvlc
  • This is the prefix that I have used sudo -u julian DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path="/run/user/1000/bus" ***Code to play sound***
  • The codes to play sound are: paplay lowBattery2.wav --volume 65536 and cvlc --play-and-exit lowBattery.mp3
  • Both codes with prefix works correctly BUT when exists another sound is playing (For example youtube) the command line returns an error.

概要:

我的问题:

  • 我无法同时播放来自守护程序的声音和其他声音(例如youtube)

我的目标:

  • 当我的电脑电池电量低时,执行此守护程序以打开通知。

就是这样。

这是script.sh

     battery=$(cat /sys/class/power_supply/BAT0/capacity)
    
        batteryLimit=97
    
        user=julian

userID=1000 
sayHelloMessage="Hi ${user}!"
messagePart1="Your computer has "
messagePart2="% of battery, please connect it"
finalMessage=$messagePart1$battery$messagePart2

while true; do
        if [ "$batteryLimit" -gt "$battery" ]; then
                echo $finalMessage;
                echo $user
                echo $userID
                sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/${userID}/bus" notify-send "${sayHelloMessage}" "${finalMessage}"
                ##sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path="/run/user/${userID}/bus"  cvlc --play-and-exit  lowBattery.mp3;
                sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path="/run/user/${userID}/bus" paplay  lowBattery2.wav --volume 65536
                ##ffplay -autoexit lowBattery.mp3;
                ##paplay lowBattery2.wav --volume 65536
                echo "Sound ended"

        else
                echo "Your battery is greater than ${batteryLimit+1} you are ok!";
        fi
        echo $(date);
        sleep 10;
done

这是守护程序:

[Unit]
Description=To show notifications when battery is smaller than 18%

After=multi-user.target

[Service]
Type=simple

ExecStart= sh /etc/systemd/system/LowBatteryScripts/script.sh

User=julian

WorkingDirectory=/etc/systemd/system/LowBatteryScripts

StandardOutput=syslog
StandarError=syslog

[Install]
WantedBy=multi-user.target

错误的日志(带有“前缀”)为: 对于cvlc:

[000055d038a38c10] alsa音频输出错误:无法打开ALSA设备“默认”:Dispositivo o recurso ocupado [000055d038a38c10]主音频输出错误:跌落声 [000055d038a38c10]主音频输出错误:音频“默认”没有设置: Dispositivo o recurso ocupado [000055d038a38c10]主音频输出错误:模块不起作用 [00007f1164c1fb50]主解码器错误:创建音频输出失败

对于paplay: 连接失败:连接被拒绝

pa_context_connect()失败:连接被拒绝

提前致谢