简体   繁体   中英

Pygame audio won't start properly unless run as root

So I am working on an alarm clock project to run as part of a homebridge automation project, and I am writing the python script that will do the actual audible alarm. This script will be started by homebridge and will then take on all of the playing sounds and snoozing and such. Essentially all I am working on now is playing sound. I have written the following simple script which uses pygame as an audio player, so that my future state machine can take care of watching for the snooze button and such.

import pygame
import time

files = ['s1.mp3']

pygame.mixer.init()
pygame.init()






stepper = 0
#file loading
while stepper < len(files):
    pygame.mixer.music.load(files[stepper])
    print("Playing:",files[stepper])
    stepper += 1
    pygame.mixer.music.play()
#play and pause
    while pygame.mixer.music.get_busy():
        timer = pygame.mixer.music.get_pos()
        time.sleep(1)
        control = input("enter control: ")
        pygame.time.Clock().tick(10)
        if control == "pause":
            pygame.mixer.music.pause()
        elif control == "play" :
            pygame.mixer.music.unpause()
        elif control == "time":
            timer = pygame.mixer.music.get_pos()
            timer = timer/1000
            print (str(timer))
        elif control == "exit":
            print ("True")
            pygame.mixer.music.stop()
            break
        else:
            continue

When I run this on windows, it works great. I am able to play, pause, check time, and most importantly, actually hear sound. When I moved this to my raspberry pi, however, it dies, but only if it is started by a regular user, in which case it gives this error

Hello from the pygame community. https://www.pygame.org/contribute.html
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM default
Traceback (most recent call last):
  File "musictest.py", line 6, in <module>
    pygame.mixer.init()
pygame.error: No available audio device

If I run it as root, ie sudo python ./musicTest.py I have no issues and the sound plays. I would prefer to not have to run this script as root, nor do I think it should really need to be. Maybe I am missing something here and it actually is necessary, but I would love a little help understanding it.

So far I have tried updating all drivers and packages to see if that fixed it. I kind of stopped trying once I realized it worked as root which means the software all works, there is just some kind of permissions issue. I am able to play audio from both HDMI and the headphone port. It is not a volume issue as that also works as root.

I also tried this page, but that didn't fix it either. If anybody has any ideas, I am open to all suggestions, or explanations as to why I must use sudo :) Thanks!

PS I am on a new image of raspbain lite that I installed today, running on a raspberry pi 2 B+, I am using python 3.7.3, and pygame 1.9.something, can't seem to find the exact number.

So I have done a bit more tinkering and I found that the issue only happened when my user was logged in on the console. I was trying to play the music over SSH, or rather, start the script over ssh. It turns out that the workaround is to add the user to the audio group giving them permission to use the audio drivers even when they are not logged in. this can be done with the command sudo adduser $USER audio . I, however, replaced $USER with my user (cbearda3). I found this on another stack overflow post once I realized that the issue was only over ssh. This was my solution, and I thought I would document it for any future person with this issue, though I realize that my use case is very specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM