Custom startup and shutdown sounds

Since I decided to make my Volumio play random Portal 2 turret sounds on startup and shutdown I decided to show you how you yourself can make your own Volumio instance play random sounds on startup and shutdown (if you also want the turrent sounds fetch them from here: https://theportalwiki.com/wiki/Turret_voice_lines)

It’s overall really simple. You need root access (sudo works too of course) to the machine however.

First pick a directory where you want to store the script and the files. For this tutorial I’ll use /opt/sounds but it can be essentially any path you want to use.

First create the directory you want to use and create the sub directories startup and shutdown:

$ mkdir -p /opt/sounds/{startup,shutdown}

Next we create the script that plays the files:

/opt/sounds/sounds.sh:

#! /bin/bash

# Variables
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
SECTION="${1:-startup}"
FOLDER="$DIR/$SECTION"
AUDIO_DEVICE="$(jq -r ".outputdevice.value" /data/configuration/audio_interface/alsa_controller/config.json)"
AUDIO_MIXER="$(jq -r ".mixer.value" /data/configuration/audio_interface/alsa_controller/config.json)"

# Script
[ ! -d "$FOLDER" ] && exit 1

volumio pause >/dev/null 2>&1

amixer -q -c "$AUDIO_DEVICE" sset "$AUDIO_MIXER" 40%
aplay -q --device=plughw:"$AUDIO_DEVICE",0 $(ls -d "$FOLDER"/* | shuf -n 1)

Don’t forget to make it executable:

$ chmod +x /opt/sounds/sounds.sh

This script will automatically use the same audio device you configured in Volumio. If you want to adjust the volume it plays the sounds, change the 40% value in the script.

Now it’s time to add the sounds.
Place any amount of sounds you want in the two previously created folders. The script will pick one of them at random every time it is called.
To test run /opt/sounds/sounds.sh startup for the startup sounds and /opt/sounds/sounds.sh shutdown

If that is working all that’s left to do is create a service that actually runs the script on startup and shutdown.
For that create the file /etc/systemd/system/sounds.service as root:

[Unit]
Description=Plays startup and shutdown sounds

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/opt/sounds/sounds.sh startup
ExecStop=/opt/sounds/sounds.sh shutdown

[Install]
WantedBy=sound.target

Lastly run those three commands as root and you’re set:

systemctl daemon-reload
systemctl enable sounds.service
systemctl start sounds.service

If everything worked alright it should have played one of the sounds after you ran the last command. Now everytime you shut it down properly and start it up one of the sounds you placed in the folders will play. You can change them any time you want.
And lastly I recommend turing the built in startup sound off.

Looking at it, I believe this belongs in https://community.volumio.com/c/help-and-support/guides, not here.
Could a moderator move it there?

Thank you very much!