stefano agresti
stefano agresti

Reputation: 21

No sound on Raspbian when using root

I'm having a really weird issue on my Raspberry. I need to play sounds with it and I need to do it inside a script which requires sudo to work. However, for some reason, when I try to play sounds with sudo, it doesn't work. No errors, it just doesn't make any sound (I tried with several libraries, none worked). It does work if I'm using omxplayer. If I don't use sudo, everything works fine. Anybody had a similar issue?

Edit: I added one of the code I used (this works just fine on another Raspberry)

import pygame
import time

pygame.mixer.init()
pygame.mixer.music.stop()
pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play()

print("Going forward")

while True:
    time.sleep(1)

Upvotes: 2

Views: 1619

Answers (3)

tedz2usa
tedz2usa

Reputation: 96

I found it extraordinarily difficult to coerce raspberry pi's root user to play audio. I needed to do this because I wanted to run a python program on startup, which plays audio via the python playsound module.

I found out that you need to do two things in order to get the root user to play audio:

  1. Use the sudo -u command to run a command as a regular user, i.e., sudo -u pi.
  2. Correctly set the XDG_RUNTIME_DIR environment variable for that user, i.e., XDG_RUNTIME_DIR=/run/user/1000.

Here's how to do it.

My Solution

First, in the regular user shell, discover what the correct value is for the XDG_RUNTIME_DIR variable.

$ echo $XDG_RUNTIME_DIR

For me, I got the output: /run/user/1000

We'll then set this value correctly before calling our script.

Get into the superuser root shell.

$ sudo -s

You are now in the root shell. This is the command to run a python script that plays audio.

$ sudo -u pi bash -c "XDG_RUNTIME_DIR=/run/user/1000 python3 /home/pi/myscript.py"

Note, you'll want to replace /run/user/1000 with whatever is the correct value for your situation.

Other Notes

Virtual Environments

If you need to use a virtual environment, created by venv or virtualenv, you can use the following to invoke your script with the correct virtual environment, (assuming your virtual environment folder is located at /home/pi/my-path-to/venv ):

$ sudo -u pi bash -c "XDG_RUNTIME_DIR=/run/user/1000 /home/pi/my-path-to/venv/bin/python3 /home/pi/myscript.py"

Run Script on Startup

If you want to run the python script on startup, I found you can just use a cronjob.

Run crontab -e:

$ crontab -e

(You may be prompted to choose a terminal editor).

Then add the following line to the file, then save:

@reboot XDG_RUNTIME_DIR=/run/user/1000 python3 /home/pi/myscript.py

Upvotes: 0

Spent nearly a day trying to enable the analog out when using the root user.

First make sure the default user (pi) has audio:

aplay sound.wav

If that works then copy the audio configuration to the root user home, try this:

sudo su
cd
cp /home/pi/.asoundrc .asoundrc
reboot

Upvotes: 1

AdamKnube
AdamKnube

Reputation: 1

Are you using the 3.5mm jack or HDMI? One thing I have noticed in the past is that if I run alsamixer as user=pi the default device is the 3.5mm aux jack. However if I run sudo alsamixer the default device is now HDMI. Never looked in to why, just know that's a thing.

Upvotes: 0

Related Questions