2

I'm developing software for Raspbian which plays some audio but when I run it with sudo the audio doesn't play through the speakers.

The same happens with the speaker-test command:

speaker-test works as expected. However sudo speaker-test runs but there is no sound.

How can I have sound working with sudo?

  • 1
    Is there a pulse-audio service running. If so, and it is set up for a per-user configuration, (which has been the default for a while now) then the hardware is locked to the user that logged in first - and this is one thing that the super-user - who you (temporarily) seem to become when using sudo can not then use because it/the audio hardware is locked to the "un-sudo" identity. Nevertheless, there could be ways around this, it is just that it is probably a security mechanism to stop one user (say a remote login via ssh) from interfering with another's prior use/access to the audio hardware. – SlySven Dec 30 '15 at 1:15
  • Think of a literal big brother in a multi-RPi networked trying to listen-in via a microphone(attached to a USB audio dongle obviously as the Pi doesn't have sound inputs) or sending THEIR choice of Thrash Metal music to your Classical music configured ears! – SlySven Dec 30 '15 at 1:16
  • Thank you, I understand. So what could I do to work around this? – JoeyCK Dec 30 '15 at 8:55
  • 1
    I think you might be looking at this the wrong way. Rather than trying to get sound as root, a better way might be to get your software to run as a normal user. In my experience, there are very very few use cases where a program needs to run as root. You might be better off with a Stack Overflow question such as 'I am trying to solve XXX problem, how do I do this without needing it to run as root?' – Skrrp Dec 30 '15 at 14:03
  • @Skrrp I understand that my software should be able to run as a normal user but in my case I'm developing it for an art installation and it won't be used in any other device. It needs to run as root because it depends on another software called Electron that currently has a bug where some features only work when running as root. – JoeyCK Dec 30 '15 at 21:28
1

One way to solve the problem would be to have your program run as root, but have it call the sound emitter as a normal user.

From the shell, it should be possible to call the command-line media player mplayer. This question gives more options on playing sound from the command line.

To make the bridge between programs, you should write a simple shell script that takes the sound file as input:

#! /bin/sh
mplayer $1

and call it with:

playsound.sh sound-to-play.mp3

This question details how to run commands as a different user without the need for a password. Add the following to /etc/sudoers:

your-sound-playing-user ALL=(root) NOPASSWD: /home/your-sound-playing-user/playsound.sh

From your root privileged application, you can then place a system call to:

sudo -u your-sound-playing-user /home/your-sound-playing-user/playsound.sh sound-to-play.mp3
| improve this answer | |
  • It is not working for me. If i use sudo -u pi ... - the output is without error but without sound either... – United121 May 3 '19 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.