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

Python • Re: PulseAudio and Root User Headaches

$
0
0
  1. Does you code need to be run as root? If not don't do so.
  2. Depending on which OS and which release of it, your Pi may not be using Pulse Audio. RPiOS Bookworm, for example, defaults to using Pipewire.
  3. Handle the error in your code rather than letting it barf. In pseudo code:

    Code:

    while true:    if sound is not available:        sleep for a bit    else:        break
    Or in pseudo python:

    Code:

    import timewhile true:    try:        # do something that will trigger the exception here        break    except Exception:        time.sleep(5)
  4. redvli has given you the best solution if you don't want to do that.
  5. For a hacky solution, set your Pi to boot to command line with automatic login and run your code from your $HOME/.bashrc. The downside is you will need to take steps to stop your process running more than once as your .bashrc gets run at every login.
    For exampe:

    Code:

    if [ `tty` = "/dev/tty1'"; then    # only run if logged in on the first physical terminal (yes, even if no monitor is connected)    # run your code herefi
  6. For an even more hacky work around (favoured by some) insert a suitable fixed length delay at the start of your code. Less than ideal as the same length won't work in all cases and it masks rather than fixes the problem.
Personally, I'd go with a combination of 3 and 4. Or determine which existing service actually starts sound and make my custom service depend on that.

[shameless self promotion]
Some further reading:
Autostart with labwc
Running A Program At Start UP A Beginner's Guide
Cron A Beginner's Guide
[/shameless self promotion]

Statistics: Posted by thagrol — Sat Dec 07, 2024 6:01 pm



Viewing all articles
Browse latest Browse all 8026

Trending Articles