I have just terminated a nice driver to use internal MP3 file and to send them over a Raspberry Pi to annonce people arriving and other messages in the house. I was looking for this for quite some time.
But then, as I wanted to make this application as a SERVICE to run on the pi, I realised that I cannot make any sound from a SERVICE. I tried different setup configuration but none worked.
I found the omxplayer that can run and play MP3 in a SERVICE, but this omxplayer has bug since 2012 that does not play the last 2 seconds of every MP3 file. So I cant use it.
Anybody here does know how to play MP3 from a SERVICE in a raspberry Pi?
Pi's and/or Raspbian have a crippled (IMO) sound system. It's likely your service is starting before Raspbian has set up alsa. If you happen to have PulseAudio installed then you have to delay your setup until the 'pi' user is set up, well past the time systemd has finished. I'm still working on the latter problem - it involves ~/.config/lxsession/LXDE-pi/autostart . If your running headless then many things may not work.
Actually, it is not 100% ok. If I boot without a moniteur, it seems not to create the pi user. And the pygame mixer reported no available audio device.
Yes. There are several definitions of headless. 1) Without Monitor and 2) without X11. You are probably using 1). What you (and I) need to learn is when and where Raspbian initializes the sound system when booting and set things up your way there OR use the .config/lxsession/LXDE startup. Most Window Managers have some sort of 'run this script when user logs in' ability. User 'pi' does log in. I'm using Raspbian buster so it's LXDE for me.
I think the when is never in the case of headless. I have my service retrying every 10 seconds and it never succeed.
If it is like in Windows, we should be able to use a "system" user or something like that. But still, the audio driver have to be loaded, or we must call something to load it.
The idea here is to use the pi with only a power supply and the audio connection. It's going to be "his life"
How is the audio connected - the pi's jack or bluetooth? I can run my pi's headless if I ssh in after booting and set things up - mixer settings and such. Then my code to use audio works. The pi boots as user 'root' and eventually auto logins in user 'pi'. If you configured your pi to boot and not automatically start the Windowing system then you will have extra work ahead.
Yes I want to use the pi's jack.
I just tried (found on the net) adding hdmi_safe=1 in the config.txt and at least now, my python script loads and run. But still no sound. The amixer complains that there are no "Headphone" and something invalid MIT-MAGIC-COOKIE-1 keyxcb_connection_has_error()
Off hand, I don't know what that hdmi_safe setting means. The amixer error is the important clue. I suspect the COOKIE error has to do with the XAUTHORITY in your tcpserver.service file.
I'd look in that config.txt to see if the headphone jack is the select audio sink or use raspi-config to check. Linux audio is a real mess under the covers and raspbian maintainers aren't helping that problem. Eventually it will work.
ok, here it is...
without the hdmi_safe=1, when I boot without a monitor, my app doesnt run. The raspi-config shows only Headphone as available audio.
I get these errors in the journal;
Summary
jun 06 17:17:52 raspberrypi systemd[1]: tcpserver.service: Service RestartSec=10s expired, scheduling restart.
jun 06 17:17:52 raspberrypi systemd[1]: tcpserver.service: Scheduled restart job, restart counter is at 22.
jun 06 17:17:52 raspberrypi systemd[1]: Stopped Mini webserver to play MP3 in Pi from Hubitat.
jun 06 17:17:52 raspberrypi systemd[1]: Started Mini webserver to play MP3 in Pi from Hubitat.
jun 06 17:17:52 raspberrypi python3[1057]: pygame 1.9.4.post1
jun 06 17:17:52 raspberrypi python3[1057]: Hello from the pygame community. https://www.pygame.org/contribute.html
jun 06 17:17:55 raspberrypi python3[1057]: Starting web server on port: 12345
jun 06 17:17:55 raspberrypi python3[1057]: amixer: Mixer attach default error: No such file or directory
jun 06 17:17:55 raspberrypi python3[1057]: xcb_connection_has_error() returned true
jun 06 17:17:55 raspberrypi python3[1057]: Traceback (most recent call last):
jun 06 17:17:55 raspberrypi python3[1057]: File "tcpserver.py", line 62, in
jun 06 17:17:55 raspberrypi python3[1057]: class MyTCPHandler(socketserver.BaseRequestHandler):
jun 06 17:17:55 raspberrypi python3[1057]: File "tcpserver.py", line 69, in MyTCPHandler
jun 06 17:17:55 raspberrypi python3[1057]: mixer.init()
jun 06 17:17:55 raspberrypi python3[1057]: pygame.error: No available audio device
jun 06 17:17:55 raspberrypi systemd[1]: tcpserver.service: Main process exited, code=exited, status=1/FAILURE
jun 06 17:17:55 raspberrypi systemd[1]: tcpserver.service: Failed with result 'exit-code'.
and when I put hdmi_safe=1 in the config.txt, after a reboot the system "thinks" I have a monitor even is there is none attached. The raspi_config shows HDMI1, HDMI2 and Headphone as available audio. My app is running but no sound. The journal shows;
Summary
-- Logs begin at Thu 2019-02-14 05:11:59 EST, end at Sat 2020-06-06 17:22:28 EDT. --
jun 06 17:21:31 raspberrypi systemd[1]: Started Mini webserver to play MP3 in Pi from Hubitat.
jun 06 17:21:38 raspberrypi python3[590]: pygame 1.9.4.post1
jun 06 17:21:38 raspberrypi python3[590]: Hello from the pygame community. https://www.pygame.org/contribute.html
jun 06 17:21:38 raspberrypi python3[590]: Starting web server on port: 12345
jun 06 17:21:38 raspberrypi python3[590]: amixer: Unable to find simple control 'Headphone',0
jun 06 17:21:39 raspberrypi python3[590]: Invalid MIT-MAGIC-COOKIE-1 keyxcb_connection_has_error() returned true
Not too sure what to look for from this point. I'll check if I cannt use a different user to boot.. but how do you automatically start the Windowing without any monitor?
I don’t think it is a question of timing because in the first case because the system tries to reload my app every 10 seconds and it never succeed.
In the second case, the error we see is only related to raising the volume with amixer at 100%. The real sound command is sent without error, but without sound
After a while, I get a timeout from alsa (which I don’t have). I’m assuming that pygame default to alsa if it can’t find a sound driver. Or something like that... not to sure.
I think I need a break.... it’s been a long day with this...
Which one is selected for audio out? What does alsamixer show. If it's not headphones, select it with the mixer, adjust volume and hope it sticks after a reboot. You're getting close.
SUCCESS: The secret was in raspi-config, I selected the option "Console autologin text console, automatically logged as pi user" then perfect. it boots and it plays sounds...