Categories
Linux Mastering Development

Play a sound from a daemon Linux

I have Manjaro Linux and I am trying to play a sound (mp3 and wav) using a script from a daemon.
Basically, I have a script.sh with a command trying to play the sound. This script is called from a daemon. (Below more details over the code).

Quick notes:

  • 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.

Summary:

My problem:

  • I can NOT play a sound from a daemon parallelly with another sound (For example youtube)

My goal:

  • Execute this daemon to turn on my notifications when the battery of my pc is low.

And that’s it.

This is the 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

And this one is the daemon:

[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

The logs of the errors (With "prefix") are:
for cvlc:

[000055d038a38c10] alsa audio output error: cannot open ALSA device "default": Dispositivo o recurso ocupado
[000055d038a38c10] main audio output error: Falló salida de audio
[000055d038a38c10] main audio output error: El dispositivo de audio «default» no se puede usar:
Dispositivo o recurso ocupado
[000055d038a38c10] main audio output error: module not functional
[00007f1164c1fb50] main decoder error: failed to create audio output

for paplay:
Connection failed: Connection denied

pa_context_connect () failed: Connection denied

Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *