After trying every possible solution online I am posting this question. I have a python script that runs on boot with the help of systemd. Everything worked fine till I installed a fresh raspian buster. Now I am able to run the python program via both systemd and through Thonny Editor. But the difference is that systemd doesn't give any audio output while the editor does. I tried to run my python script via terminal and the audio was playing without any issues.
Now please note that I have another raspberry pi which is still running the older version of the OS with the same systemd configuration and everything works fine in it. Now I could have easily cloned that sd card and used it but that sd card is 16 GB and I want to migrate to a 32 GB sd card(I tried cloning, pi didn't boot up)
This is the systemd service file
[Unit]
Description=My Service
After=network.target
[Service]
ExecStart=/usr/bin/python3 -u Main.py
WorkingDirectory= /home/pi/xyz
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy = default.target
Systemd runs the python file without any errors but with no audio output. Please help me out.
-
What does the script do? Does it endless play a sound in the background? Do you use pulseaudio?Ingo– Ingo2021年01月10日 18:44:06 +00:00Commented Jan 10, 2021 at 18:44
-
@Ingo The script plays a welcome sound when started and then will play sound whenever the push buttons are clicked. Very basic stuff.Nitesh Singh– Nitesh Singh2021年01月11日 02:26:00 +00:00Commented Jan 11, 2021 at 2:26
-
1I am having the same problem exactly. Was a solution ever found? I can get it to work via a .desktop file, but I'd really like to use functionality of systemd.Peter Wiley– Peter Wiley2021年05月19日 20:16:42 +00:00Commented May 19, 2021 at 20:16
-
@PeterWiley No solution was found. I had to shift to the older version of Raspian OS. Because after updating this problem started for me.Nitesh Singh– Nitesh Singh2021年05月20日 08:10:19 +00:00Commented May 20, 2021 at 8:10
-
1I know, I'm the one who submitted the answer.Peter Wiley– Peter Wiley2021年05月21日 16:03:10 +00:00Commented May 21, 2021 at 16:03
2 Answers 2
The answer to this issue can be found here:
https://www.raspberrypi.org/forums/viewtopic.php?t=278665
The solution presented worked for me to get sound while running a python program from system.
What one has to do is to add defaults to the asound.conf file at /etc/asound.conf. I did sudo nano on file and found nothing in it, so I added
defaults.pcm.card 1
defaults.pcm.card 1
Why "1"? It's the number given when one does
pi@xxxx:~ $ cat /proc/asound/cards
0 [b1 ]: bcm2835_hdmi - bcm2835 HDMI 1
bcm2835 HDMI 1
1 [Headphones ]: bcm2835_headphonbcm2835 Headphones - bcm2835 Headphones
bcm2835 Headphones
In my case I wanted sound out of the headphone jack.
-
Thank you so much for the answer.Nitesh Singh– Nitesh Singh2021年05月21日 08:17:37 +00:00Commented May 21, 2021 at 8:17
This service file worked for me... Note how it starts after After=sound.target
[Unit]
Description=FireHouse Game
After=sound.target
[Service]
Type=simple
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/pi/.Xauthority"
ExecStart=/usr/bin/python3 /home/pi/FireGame/fire_game.py
Restart=on-abort
KillMode=process
SendSIGHUP=nos
[Install]
WantedBy=graphical.target
Explore related questions
See similar questions with these tags.