gjs31
Posts: 25
Joined: Sat Nov 04, 2017 2:42 am

Play audio file in python from command line using sudo

Sat Jul 13, 2019 7:38 am

I'm working on the code for a rover, which has neopixels on board which need to run using sudo.

I'm trying to add audio clips to buttons in the code, but the audio doesn't play. Stripping the neopixel stuff out of the code lets me run it without sudo and audio plays fine, but running this stripped down version using sudo gives no sound output again.

Is there's any way around this? have spent most of the day on google trying to find a solution, but nothing seems to solve it.

Thanks.

User avatar
Michiel O.
Posts: 178
Joined: Mon Dec 12, 2016 12:06 pm

Re: Play audio file in python from command line using sudo

Sat Jul 13, 2019 1:36 pm

Wild guess. Sudo can not only impersonate root, but any user. So if your ordinary user is pi, this could perhaps work:

Code: Select all

sudo -u pi aplay buttonclick.wav
It might be a path related thing, but without seeing code and/or error messages it's hard to guess.
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes

gjs31
Posts: 25
Joined: Sat Nov 04, 2017 2:42 am

Re: Play audio file in python from command line using sudo

Sat Jul 13, 2019 9:57 pm

Thanks for that.

It worked perfectly for the python script that just had an os call to the audio file, but when I used the same approach on the script with the neopixel functionality I get the error stating it needs to run in sudo:

Code: Select all

sudo -u pi python3 roger.py

Returns:

Code: Select all

Can't open /dev/mem: Permission denied
Traceback (most recent call last):
  File "roger.py", line 8, in <module>
    led.led_init()
  File "/home/pi/Desktop/Roger/led.py", line 12, in led_init
    pixels.fill((0, 0, 0))
  File "/usr/local/lib/python3.7/dist-packages/neopixel.py", line 214, in fill
    self.show()
  File "/usr/local/lib/python3.7/dist-packages/neopixel.py", line 230, in show
    neopixel_write(self.pin, self.buf)
  File "/usr/local/lib/python3.7/dist-packages/neopixel_write.py", line 24, in neopixel_write
    return _neopixel.neopixel_write(gpio, buf)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/bcm283x/neopixel.py", line 63, in neopixel_write
    raise RuntimeError("NeoPixel support requires running with sudo, please try again!")
RuntimeError: NeoPixel support requires running with sudo, please try again!
swig/python detected a memory leak of type 'ws2811_t *', no destructor found.

Its the exact same error as just trying to call the program without sudo.

User avatar
Michiel O.
Posts: 178
Joined: Mon Dec 12, 2016 12:06 pm

Re: Play audio file in python from command line using sudo

Sun Jul 14, 2019 10:30 am

Are you sure the problem is related to playing sound? In the stack trace I see:

File "/usr/local/lib/python3.7/dist-packages/neopixel_write.py", line 24, in neopixel_write
return _neopixel.neopixel_write(gpio, buf)
File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/bcm283x/neopixel.py", line 63, in neopixel_write
raise RuntimeError("NeoPixel support requires running with sudo, please try again!")


That suggests to me that neopixel_write() fails because it wants to set a GPIO pin, and (depending on the I/O library you use) that needs root privileges.
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes

Return to “Python”