1

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.

asked Jan 10, 2021 at 17:10
6
  • What does the script do? Does it endless play a sound in the background? Do you use pulseaudio? Commented 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. Commented Jan 11, 2021 at 2:26
  • 1
    I 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. Commented 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. Commented May 20, 2021 at 8:10
  • 1
    I know, I'm the one who submitted the answer. Commented May 21, 2021 at 16:03

2 Answers 2

3

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.

answered May 20, 2021 at 12:50
1
  • Thank you so much for the answer. Commented May 21, 2021 at 8:17
0

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
Darth Vader
4,21824 gold badges47 silver badges71 bronze badges
answered Feb 7, 2024 at 4:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.