Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5273

Beginners • Re: Getting audio to play on bluetooth speaker with sudo

$
0
0
post the os your using (desktop/lite/32/64) &
your script your trying to run.
My device is a Pi Zero 2 W. The OS is Raspberry PI OS (64-bit), Bookworm. I followed the instructions here from butchkemper:viewtopic.php?t=359240 to ensure that there was enough memory to update the entire system. I've checked that all of the libraries and firmware are updated.

Here's an example of the code I'm attempting to run:

Code:

import boardimport neopixelfrom gpiozero import Button#Play sounddef playsound():    sound = pygame.mixer.Sound('Media/sound_effect.wav')    sound.play()    time.sleep(4)if __name__ == '__main__':    pygame.init()    button = Button(17)        pixels = neopixel.NeoPixel(board.D18, 10, brightness = 255)        #Set the NeoPixels to blue by default    pixels.fill((0, 0, 255))    while True:        if button.is_pressed:            playsound()            pixels.fill((255, 0, 0))
Even before making the change of having pulseaudio run system-wide, this code was not working as intended.

I've tried four different cases.

If I run the script as normal (python3 script.py), I get the above error where I need sudo in order for NeoPixels to run.

If I run the script as sudo (sudo python3 script.py), there are no runtime errors, and the NeoPixels light up as intended. In fact, when I the press the button, you can tell it triggers the time.sleep(4) before switching the colors. This leads me to believe it's attempting to output audio but perhaps to a source that isn't the bluetooth speaker.

This is the part that leads me to believe it has something to do with sudo not recognizing the bluetooth speaker.

If I take out the neopixels part altogether, and run the code normally (python3 script.py) the sound plays over the speaker after the button hits as intended.

If I take out the neopixels part, and run the code as sudo (sudo python3 script.py), then once again no sound gets output to the bluetooth speaker.

Statistics: Posted by buttmunky — Fri May 03, 2024 12:08 am



Viewing all articles
Browse latest Browse all 5273

Trending Articles