How to play a sound from a systemd service on Ubuntu?

I try to play a sound from a service on Ubuntu:

tim@timpc:~$ uname -a Linux timpc 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Here is the service definition:

tim@timpc:~$ cat /etc/systemd/system/alarmd.service
# Datei /etc/systemd/system/alarmd.service
[Unit]
Description=alarmd service
Wants=sound.target
After=sound.target

[Service]
Type=oneshot
Environment="DISPLAY=:0"
User=tim
;ExecStart=/home/tim/Dokumente/super/alarmd/build/release/dist/alarmd --sound /usr/local/share/alarmd/a.wav
ExecStart=mplayer /usr/local/share/alarmd/a.wav

[Install]
WantedBy=multi-user.target

I start the service like this:

sudo systemctl start alarmd.service

...and get this output:

tim@timpc:~$ journalctl -S today -u alarmd.service
-- Logs begin at Wed 2021-08-11 09:41:59 CEST, end at Sun 2021-12-05 16:28:06 CET. --
Dez 05 16:28:06 timpc systemd[1]: Starting alarmd service...
Dez 05 16:28:06 timpc mplayer[13082]: MPlayer 1.3.0 (Debian), built with gcc-9 (C) 2000-2016 MPlayer Team
Dez 05 16:28:06 timpc mplayer[13082]: do_connect: could not connect to socket
Dez 05 16:28:06 timpc mplayer[13082]: connect: No such file or directory
Dez 05 16:28:06 timpc mplayer[13082]: Failed to open LIRC support. You will not be able to use your remote control.
Dez 05 16:28:06 timpc mplayer[13082]: Terminal type `unknown' is not defined.
Dez 05 16:28:06 timpc mplayer[13082]: Playing /usr/local/share/alarmd/a.wav.
Dez 05 16:28:06 timpc mplayer[13082]: libavformat version 58.29.100 (external)
Dez 05 16:28:06 timpc mplayer[13082]: Audio only file format detected.
Dez 05 16:28:06 timpc mplayer[13082]: Load subtitles in /usr/local/share/alarmd/
Dez 05 16:28:06 timpc mplayer[13082]: ==========================================================================
Dez 05 16:28:06 timpc mplayer[13082]: Opening audio decoder: [pcm] Uncompressed PCM audio decoder
Dez 05 16:28:06 timpc mplayer[13082]: AUDIO: 44100 Hz, 2 ch, s16le, 1411.2 kbit/100.00% (ratio: 176400->176400)
Dez 05 16:28:06 timpc mplayer[13082]: Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)
Dez 05 16:28:06 timpc mplayer[13082]: ==========================================================================
Dez 05 16:28:06 timpc mplayer[13082]: AO: [pulse] Init failed: Connection refused
Dez 05 16:28:06 timpc mplayer[13082]: Failed to initialize audio driver 'pulse'
Dez 05 16:28:06 timpc mplayer[13082]: [AO_ALSA] alsa-lib: pcm_hw.c:1715:(snd_pcm_hw_open) open '/dev/snd/pcmC0D0p' failed (-2): No such file or directory
Dez 05 16:28:06 timpc mplayer[13082]: [AO_ALSA] alsa-lib: pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
Dez 05 16:28:06 timpc mplayer[13082]: [AO_ALSA] Playback open error: No such file or directory
Dez 05 16:28:06 timpc mplayer[13082]: Failed to initialize audio driver 'alsa'
Dez 05 16:28:06 timpc mplayer[13082]: [AO SDL] Samplerate: 44100Hz Channels: Stereo Format s16le
Dez 05 16:28:06 timpc mplayer[13082]: [AO SDL] using aalib audio driver.
Dez 05 16:28:06 timpc mplayer[13082]: [AO SDL] Unable to open audio: No available audio device
Dez 05 16:28:06 timpc mplayer[13082]: Failed to initialize audio driver 'sdl:aalib'
Dez 05 16:28:06 timpc mplayer[13082]: Could not open/initialize audio device -> no sound.
Dez 05 16:28:06 timpc mplayer[13082]: Audio: no sound
Dez 05 16:28:06 timpc mplayer[13082]: Video: no video
Dez 05 16:28:06 timpc mplayer[13082]: Exiting... (End of file)
Dez 05 16:28:06 timpc systemd[1]: alarmd.service: Succeeded.
Dez 05 16:28:06 timpc systemd[1]: Finished alarmd service.

How should I define the service so that the sound is played?

The command itself works:

tim@timpc:~$ mplayer /usr/local/share/alarmd/a.wav
MPlayer 1.3.0 (Debian), built with gcc-9 (C) 2000-2016 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing /usr/local/share/alarmd/a.wav.
libavformat version 58.29.100 (external)
Audio only file format detected.
Load subtitles in /usr/local/share/alarmd/
==========================================================================
Opening audio decoder: [pcm] Uncompressed PCM audio decoder
AUDIO: 44100 Hz, 2 ch, s16le, 1411.2 kbit/100.00% (ratio: 176400->176400)
Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)
==========================================================================
AO: [pulse] 44100Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A:   9.1 (09.1) of 233.0 (03:53.0)  0.0% 


MPlayer interrupted by signal 2 in module: play_audio
A:   9.2 (09.1) of 233.0 (03:53.0)  0.0% 

Exiting... (Quit)

In current versions of Ubuntu, typically sound is handled by the pulse audio server. If you run sound players from your logged in session, then the player will find the server through the environment variables XDG_RUNTIME_DIR and/or PULSE_RUNTIME_PATH. For a job in systemd (or cron) to be able to find the server, it will need these values.

Luckily, those values are predictable via the user name, and should not change from one login to the next if it is the same user. (Setting DISPLAY was a good try, but not the right session variable.)

For example, if user tim is the first user on the system, the user's uid is likely 1000 (check in /etc/passwd or type id or check current values.) Add to your systemd file the following: (Not sure if one of these is good enough or if you need both, it may be version dependent.)

Environment="XDG_RUNTIME_DIR=/run/user/1000"
Environment="PULSE_RUNTIME_PATH=/run/user/1000/pulse/"

Since this only works when you are logged in anyway, an alternative to a systemd file would be to start your service in a script that is started as part of your gnome-session. If you did this, you wouldn't need to worry about the session variables at all.