I'm trying to play a .wav file on my server (which is an old laptop with Ubuntu server installed) through a website using a shell script.
Now the problem is that when I play the .wav file through mplay on root, everything works just fine, but if I try to play the sound through the user www-data I get following error message:
$ sudo -u www-data sh script.sh
MPlayer 1.4 (Debian), built with gcc-11 (C) 2000-2019 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 bell.wav.
libavformat version 58.76.100 (external)
Audio only file format detected.
Load subtitles in ./
==========================================================================
Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
AUDIO: 48000 Hz, 2 ch, s16le, 40.0 kbit/2.60% (ratio: 5000->192000)
Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
==========================================================================
AO: [pulse] Init failed: Connection refused
Failed to initialize audio driver 'pulse'
Could not open/initialize audio device -> no sound.
Audio: no sound
Video: no video
with root however it works just fine:
$ sh script.sh
MPlayer 1.4 (Debian), built with gcc-11 (C) 2000-2019 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 /var/www/html/bell.wav.
libavformat version 58.76.100 (external)
Audio only file format detected.
Load subtitles in /var/www/html/
==========================================================================
Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
AUDIO: 48000 Hz, 2 ch, s16le, 40.0 kbit/2.60% (ratio: 5000->192000)
Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
==========================================================================
AO: [pulse] 48000Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A: 2.8 (02.8) of 3.0 (03.0) 0.5%
and this is how the shell script looks like:
$ cat script.sh
#!/bin/sh
mplayer '/var/www/html/bell.wav'
Now after looking through countless threads I made sure that www-data has all the permission it needs and additionally added them to the usergroups:
$ sudo -u www-data groups
www-data cdrom audio video pulse-access
I tried to just use mplay on it's own $ sudo -u www-data mplayer bell.wav
to test if there might be issues with the script but I get the same error message mentioned above.
I looked in the dmsg with sudo dmesg | grep pulse
, but there seems to be no messages for pulse. I just can't figure out what I do wrong, I am guessing it has to do something with the access permission for www-data since everything works fine if I use the exact same shell script with root.